Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grammar for Unix command line options

This is a homework question. I would like to write a simple parser for Unix command line options.
First, I would like to define a grammar with BNF.

Options = Option | Options, space, Option;
Option = OptionName | OptionName, OptionArguments; 
OptionName = '--', any character excluding '-' | OptionName, any character;
OptionArguments = OptionArgument | OptionArguments, space, OptionArgument;
OptionArgument = any character excluding '-' | OptionArgument, any character;

("any character" here is any alphanumeric character).

Does it make sense ? The next question is how to add "old" Unix options, which start with a single hyphen and can be grouped together (e.g. ls -lht)

like image 352
Michael Avatar asked Dec 11 '10 13:12

Michael


People also ask

What are Unix command options?

In the original Unix tradition, command-line options are single letters preceded by a single hyphen. Mode-flag options that do not take following arguments can be ganged together; thus, if -a and -b are mode options, -ab or -ba is also correct and enables both.

What are options in command line?

Options are a list of flags and other parameters that can control the behavior of the wt command line as a whole. Commands provide the action, or list of actions separated by semicolons, that should be implemented. If no command is specified, then the command is assumed to be new-tab by default.

What is Unix command format syntax?

The syntax for a UNIX command is broken into three parts: the command, a list of options, and a list of arguments. For the purposes of this class, all three must be done in this order, although either the options or the list of arguments may be excluded depending on what command you are using and how it is being used.


1 Answers

Just notice that the given grammar is quite ambiguous - for example, if you have a few words in a row, you wouldn't know if these are different options or an option with some arguments.

As for your second question (regarding "old" unix), you could add another rule to the grammar, something of the sort:

option -> optionGroup | (anything that was there before);
optionGroup -> '-', flags;
flags -> flag | flag, flags;
flag -> single letter;
like image 153
Eran Zimmerman Gonen Avatar answered Nov 15 '22 07:11

Eran Zimmerman Gonen