adishavit / argh

Argh! A minimalist argument handler.
BSD 3-Clause "New" or "Revised" License
1.33k stars 93 forks source link

Git-like subcommand #40

Closed zhihaoy closed 5 years ago

zhihaoy commented 5 years ago

How can a command-line interface be called "modern" without subcommands ;)

adishavit commented 5 years ago
  1. Where is Argh called modern? It proclaims it is minimal.
  2. What are subcommands?
zhihaoy commented 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.

adishavit commented 5 years ago

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.

zhihaoy commented 5 years ago

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.

zhihaoy commented 5 years ago

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.

a4z commented 5 years ago

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

zhihaoy commented 5 years ago

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.

adishavit commented 5 years ago

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.)

zhihaoy commented 5 years ago

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.

adishavit commented 5 years ago

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.

a4z commented 5 years ago

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 :)

adishavit commented 5 years ago

@zhihaoy: Check out this blog post by @a4z

a4z commented 5 years ago

https://a4z.bitbucket.io/blog/2019/04/30/Git-like-sub-commands-with-argh.html