1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Long Form and Short Form Command-line Options

Command-line Options


What Are Command-line Options?

Before we dive into OptionParser, a tool used to parse command-line options, it's first necessary to take a look at command-line options.

Command-line options are necessary in any script that's run from the command-line. While you can prompt for user input with gets, this can quickly become annoying to the user. Also, many users of the command-line expect scripts to adhere to the UNIX philosophy that once run from the command-line, programs should run unattended to completion. There are a lot of subtleties with command-line options parsing, so it's pretty difficult to get right. This is why there aren't a lot of libraries around to do this. OptionParser is quite good and is included with Ruby.

Forms of Command-line Options

Options are supplied after the script name. They can have two forms: a long form and short form. The short form is a single character, and is preceded by a single dash. For example, -v or -h).

The long form is a word or phrase preceded by a double dash, such as --verbose or --help). The short form of long options should be the first character in the word or phrase when possible. For example, -v is short for --verbose.

Option Arguments

In addition to the options themselves, options can have arguments. For example, if the -f switch tells the program which file to open, this would be written as -f FILE. Both long and short options can have an argument, such as --file FILE.

Options can either be optional or mandatory. Optional options can be supplied if the user wishes, or they can be omitted. Optional options are written in the option description in square brackets. If the option's argument is in square brackets, the option itself is optional, not just the argument. For example, if you're not required to define which file the script should open, it would be written as -f [FILE] or --file [FILE]. Mandatory options, on the other hand, must be provided each time the program is run.

Long-Form and Short-Form Options

Long-form options can also have negated forms or other forms within the definition. For example, the option --[no-]verbose means there are actually two options here:--verbose and --no-verbose.

Short-form options can be combined into a group. For instance, if I wanted the -v, -h and -i options, I could say -vhi. However, only one option with an argument can be added to a group. If I wanted to add -f [FILE] to that group, it would look like -vhif somefile.txt. After adding that option with an argument, I wouldn't be able to add any more options with arguments to that group, I'd have to start a new group.

In addition to all the options, there is also a banner. The banner is displayed at the top of the help screen. By convention, the help screen is displayed when the -h or --help option is encountered. The help screen displays the banner, a list of options, their different forms, what parameters they take and a short description of each.

Finally, there are parameters to the script itself, not part of any option. For example, if my script resizes several image files, I want these files to be listed on the command-line. These files aren't part of any switch, they're an independent part of the command-line.

  1. About.com
  2. Technology
  3. Ruby
  4. Advanced Ruby
  5. Command-line Options--Long Form and Short Form Command-line Options

©2014 About.com. All rights reserved.