Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align horizontally a multiline CLI help text with OptionParser

I am trying to make the output of my CLI Ruby gem my_command --help cleaner.

There is some CLI options and flags that need a couple of sentences to explain them. I don't have found a way to properly align this text in their column when the explanation is too long to fit inside a regular terminal width view.

I want to have something like this:

ab --help as an example ab --help as an example, note how some flags have a multiple line explanations with a proper alignment.

Right now, I am doing something like this in OptionParser to keep text aligned in their column in case we need multiple lines to explain something:

opts.on("-d", "--directory PATH", String, "Directory to save the downloaded files into\n\t\t\t\t     Default is ./websites/ plus the domain name") do |t|
  options[:directory] = t
end

It's working, but it doesn't seem optimal nor clean to have \t everywhere to force formatting. Plus, I can see cases where it's not being formatted correctly in other terminal configurations.

How can I align horizontally a multiline CLI help text with OptionParser in a clean way?

like image 677
Hartator Avatar asked Sep 07 '25 08:09

Hartator


1 Answers

You can force line breaks without needing to add tabs by adding more parameters to opts.on:

opts.on("-d", "--directory PATH", String, 
        "Directory to save the downloaded files into",
        "Default is ./websites/ plus the domain name") do |t|
  options[:directory] = t
end

This isn't very clearly documented in official documentation, but you can see it used in the complete example.

like image 165
Kathryn Avatar answered Sep 09 '25 03:09

Kathryn