Closed zhihaoy closed 5 years ago
I think command-line libraries nowadays are not relevant enough if without subcommands, because it's getting harder and harder to maintain a new command-line interface (I mean application, not library) without subcommands.
git rebase -i 821ae37
The rebase
is a subcommand, it owns a subparser to parse the rest of the dash-style arguments, values, or positional arguments.
Argh is not designed to be a total argument parsing solution. It is designed to be as non-intrusive as possible. What you seem to desire is much more comprehensive. For an extensive cli try one of the more verbose albeit powerful alternatives.
That being said, there’s nothing stopping you from creating the top level parser, check it’s first arg and creating a sub-argument parser (or several) for each subcommand.
The point of using a cmdline parsing library is not to look into argv by yourself, and I think parsing subcommands and deciding which subparser to continue should also be the library's responsibility. Adding this functionality doesn't violate any lines you wrote in "argh does not care about"; the functionality is still within the area of parsing.
To look it in a different way: subparser is just one interpretation of this feature, the goal is merely to parse positional arguments at fixed position. instead of treating argv[0]
as the command name, given some options to argh, you can get argv[1]
, always, as a subcommand name, things like that.
you can perfectly, and simple, implement this on your own with argh
int main (int argc, const char** argv)
handle argv[1]
as the sub command
feed
argc - 1, argv + 1
as the arguments for the sub command to argh
It may not always exist, for example, git --help
is a valid command-line. Playing with argc/argv externally means that you will have to test, aka parse, by yourself.
You do not need to parse anything yourself.
Just pass argv
to one parser to check for eg —help
and use cmdl[1]
to determine/choose a second parser and pass it argv
again.
Unless you have duplicate commands you should be fine (especially if the external parser commands take precedence.)
Which will essentially parse the whole command-line options twice, given the design of argh, so it's hard to say the flow is still simple.
Yes. As I mentioned argh was not designed for this (or other advanced cli usage). You are welcome to fork the project and try to add this functionality.
no, you do not need to parse everything 2 times, since argv[1] is clear and you do not need to pass this already to argh, just what follows.
In practice I have a map with key and lambdas, and argv[1] will give me the function I have to call withargc - 1, argv + 1, so it is still super simple and exactlhy what some people like and need , like me :)
@zhihaoy: Check out this blog post by @a4z
How can a command-line interface be called "modern" without subcommands ;)