parsing - Writing command line argument parsers -


Very well-known command line logic analyzer, such as argp or promotion sub-group: program + options For C ++

Example, I recently tried to write one which let me parse simple scenarios like C ++ in this way:

  int main, Char * argv []) {auto state = parse (argc, argv); Const auto foo = mandatory & lt; Int & gt; (State, {'f', "afu"}); Const. Auto Bar = Optional_V_default & lt; Int & gt; (State, {'b', 'bar'}, 42); Const Auto Frab = Optional & lt; Std :: string & gt; (State, {'f', "frau"}); If (! Frob) {...}}   

but soon found out that parsing the independent flag is not trivial (like -fgx as < Code> -f -xg ), then throwing free flagging with the value Eggus in the mix actually became trivial (like tar -xvzf frob.tar.gz ).

The problems became clear only by empirical; I did not find anything for this search

Do you know any good resources on this topic? What are your best practices?


Note: Even though I'm giving some C ++ examples, this question should be language-agnostic. I am asking for algorithms and general suggestions.

I think if you are writing a command line parser without a declarative portion (in the example) , And which allows the flags / short-choice ( -xf to be the same as the -x -f ), you order the customer call order in some way To implement.

For example, consider (It is believed that this is a valid invitation for Unix tar program):

  tar -vfxul Tar -x   

Here you have the following options:

  xvf = xul.tar   

If you < Code> x for the first time the test (p-code),

  state = parse (argc, argv) x_set = flag (state, 'x') ... Filename = mandatory (state, 'f' or 'filename')   

You get the answer to quesion in the trouble: x ?

  tar -vfxul.tar -x ^ ^ x? X?   

Because the parser is not yet aware of the file name option, it can assume that in vfxul.tar Each single letter ( v , x , u , ...) can be a recognized flag.

So I think that your best possible estimate (without resorting to simple artificial intelligence or some complex heuristics) is to take the first form of just x . I believe that you have parsed the short option with any of the options with any flag-option:

  state = parse (argc, argv) x_set = flag (state, X ') ... filename = Mandatory (state,' F 'or' filename ') // Enter the error here!   

It puts you in a situation where you have your code rotated in a certain order, which reduces the applicability of such a library, so it is actually only a For a very simple situation - the operator where you have a non-reciprocal logic list.

Note: You only need to reorder your parsing where short-option is possible or when the flag / short-option group is allowed.


source
  • amphirism

Comments