Argon: an alternative parser for command-line arguments

Markus Laker via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Wed Mar 9 10:56:10 PST 2016


On Saturday, 5 March 2016 at 16:28:25 UTC, karabuta wrote:
> I think he meant: [git status --help], where you have three 
> attributes with the last one being the flag. So in addition to: 
> [status --help] by default, you also have: [git status --help] 
> to get help on status only.

Odd: I wrote a reply to this a few days ago, and it's nowhere to 
be seen.  I'll save a copy of this reply until I see it appearing 
on the forum.

Argon doesn't directly support subcommands.  That probably stems 
from a bias of mine: that subcommands make it harder for the 
author to parse the command and to generate good error messages, 
and also that they make it harder for users to use unfamiliar 
commands, because users must read a man page that documents 
eleven things they have no interest in doing just to get to the 
one thing that they need to do in order to get on with their day. 
  At work, where I have written and I still maintain many hundreds 
of commands, I've moved away from subcommands completely: every 
operation gets a command of its own.  But I know that not 
everyone agrees with me, and that's OK.  If we want to debate 
this topic further, we should probably move the discussion from 
Announce to General.

To support git-style syntax while using Argon, I'd do this:

1. Find the (possibly empty) initial sequence of tokens that 
start with a dash.  Pass them to an Argon-derived class which 
we'll call `Stem', which parses them.

2. If no more tokens exist (as in "my-command --help"), do what 
we can with the options we've seen, and then exit.

3. Otherwise, the next token must be a subcommand name: we've 
seen something "my-command --verbose display-widgets --paginate". 
  Use that token to select a leaf class, also derived from Argon.  
There's one leaf class per subcommand.

4. Pass the remaining tokens (in this example, just "--paginate") 
to the selected leaf pass for parsing.  Also pass a reference to 
Stem, so that the leaf code can use any options garnered by Stem.

It shouldn't be hard to write some reusable code to do this, if 
it were a common requirement.


More information about the Digitalmars-d-announce mailing list