c-blake / cligen

Nim library to infer/generate command-line-interfaces / option / argument parsing; Docs at
https://c-blake.github.io/cligen/
ISC License
508 stars 24 forks source link

main + sub-commands #167

Closed genotrance closed 4 years ago

genotrance commented 4 years ago

With cligen, while you have support for sub-commands, I was curious if it is possible to have a default main command with its own flags which is not named, along with multiple named sub-commands.

This is mainly because I have an established tool with many flags and now want to add sub-commands without breaking the main tool.

c-blake commented 4 years ago

This is very similar to https://github.com/c-blake/cligen/issues/135 which I did not see a way to do cleanly, just at the CLI syntax level. My best recommendation would be to factor things such that you have a new multi-command (e.g. toaster) that has one subcommand (which could be as short as period ('.')) that corresponds to the original command (and uses all its code via e.g. import). Then people can gradually migrate over years from toast to toaster . and someday you can drop toast. (Or keep it forever.)

genotrance commented 4 years ago

Sounds fair - closing this one, will consider a separate binary for the new functionality and leave toast alone.

c-blake commented 4 years ago

Ok.

Also, just for comprehensiveness, with the above mentioned organization which requires no cligen change, you are telling users "toast" is an abbreviation for "toaster .". Another approach might be to have cligen try to use C's argv[0] to decide which subcommand is routed to. If there were a toaster -> toast symlink (or full copy) and cligen saw the argv[0] toast then it could route that way.

The problem with this approach is that argv[0] is not always reliable. While it's usually the name of the executable file, in Unix (at least) it depends upon how the exec system call was set up. Some shells will set $_ to be the name of the program file, but that is more a Bourne Shell family thing not done by, e.g., Csh. (No idea about Fish, etc.)

Anyway, this direction of ideas is mostly just saving installation disk space over the above share-code-via-import idea. It shares the same invocation possibilities for end-users, but seemed worth mentioning.