Integrate getopt for CLI parsing #18

Closed
opened 2020-10-22 13:43:10 +00:00 by decentral1se · 15 comments
Owner
No description provided.
Author
Owner

OK, been researching for a little bit. Maybe doing it ourself is the way to go as you said? All existing framework/library/wrapper things seems to be very out of date or simply abandoned and there isn't a concensus on using one single thing? https://stackoverflow.com/a/29754866 seems to point to getopt (enhanced) then? I downloaded their example of "myscript" from the SO answer and it seems to be quite robust actually. So, maybe we just need to adapt from there?

OK, been researching for a little bit. Maybe doing it ourself is the way to go as you said? All existing framework/library/wrapper things seems to be very out of date or simply abandoned and there isn't a concensus on using one single thing? https://stackoverflow.com/a/29754866 seems to point to `getopt (enhanced)` then? I downloaded their example of "myscript" from the SO answer and it seems to be quite robust actually. So, maybe we just need to adapt from there?
Author
Owner

Getopt

A more robust and flexible approach is to use enhanced getopt which is based on a C library that is capable of parsing arguments in a variety of different ways. It doesn’t care about the order and can handle spacing and quoting.

Not that getopt and getopts are two different utilities and should not be confused with one another.

Getopt is ubiquitious across Linux distributions. To check if you have the enhanced version, do this:

$ getopt --test
echo?
4

The exit code should be 4.

> Getopt > > A more robust and flexible approach is to use enhanced getopt which is based on a C library that is capable of parsing arguments in a variety of different ways. It doesn’t care about the order and can handle spacing and quoting. > > Not that getopt and getopts are two different utilities and should not be confused with one another. > > Getopt is ubiquitious across Linux distributions. To check if you have the enhanced version, do this: > > $ getopt --test > $ echo $? > 4 > > The exit code should be 4.
Owner

👍👍👍 this is exactly what I was thinking on the call, it'd at least help us with parsing -a / -e etc.

Didn't find anything on using it to help subcommands so I suspect we might need to continue using our copypasta for that.


Do we think tput is universal enough for our purposes, colour-wise?

👍👍👍 this is exactly what I was thinking on the call, it'd at least help us with parsing `-a` / `-e` etc. Didn't find anything on using it to help subcommands so I suspect we might need to continue using our copypasta for that. --- Do we think `tput` is universal enough for our purposes, colour-wise?
Author
Owner

Do we think tput is universal enough for our purposes, colour-wise?

I don't really know but it seems to have survived since the fecking '80s so that gives much much hope! Must be stable by now, right haha? Was checking https://en.wikipedia.org/wiki/Tput.

> Do we think tput is universal enough for our purposes, colour-wise? I don't really know but it seems to have survived since the fecking '80s so that gives much much hope! Must be stable by now, right haha? Was checking https://en.wikipedia.org/wiki/Tput.
decentral1se changed title from Integrating with a CLI parser so as to avoid long-term foot guns to Integrate getopt for CLI parsing 2020-10-22 17:37:10 +00:00
Owner
http://docopt.org/ maybe? Bash implementation: https://github.com/docopt/docopts Python-to-bash: https://github.com/andsens/docopt.sh
Author
Owner

Hey docopt bash version looks sick! I don't understand the limitations though. Do we face running into some issue with sub-commands or some type of option parsing? Are we better off doing our own getopt (enhanced) magic? Down to try.

Hey docopt bash version looks sick! I don't understand the limitations though. Do we face running into some issue with sub-commands or some type of option parsing? Are we better off doing our own `getopt (enhanced)` magic? Down to try.
Owner

I was wrong, docopts is Golang, single binary available, great rejoicing. Might try it out. Seems to support subcommands. I think the main drawback is that if we're going with a separate binary then why not make that binary python3 and make our lives familiar and easy, but I am still feeling OK about the Bash direction with or without docopts.

I was wrong, `docopts` is Golang, single binary available, great rejoicing. Might try it out. Seems to support subcommands. I think the main drawback is that if we're going with a separate binary then why not make that binary `python3` and make our lives familiar and easy, but I am still feeling OK about the Bash direction with or without `docopts`.
Author
Owner

Ohhhhh you mean, have the CLI wrapper written in plain Python 3 using argparse (hence, no dependencies to install) and then pass parsed commands into bash scripts or other things? I don't know if you mean this lol but fuck me that sounds like a winning approach which will horrify everyone but make us feel great forever.

Ohhhhh you mean, have the CLI wrapper written in plain Python 3 using argparse (hence, no dependencies to install) and then pass parsed commands into bash scripts or other things? I don't know if you mean this lol but fuck me that sounds like a winning approach which will horrify everyone but make us feel great forever.
Owner

I didn't mean that, but that's basically what the Python docopt version does IIUC -- down with whichever of these evil plans either of us feels like moving on first!

I didn't mean that, but that's basically what the Python docopt version does IIUC -- down with whichever of these evil plans either of us feels like moving on first!
Author
Owner

OK so current status is, you run make docopt and the Makefile will install the docopt-sh python package into a .venv and run it on the abra script. That then reads the DOC string and generates a parser blob that is updated in-place inside abra. I haven't quite finished writing the DOC (feel free if you get there! moved everything to the top and added a TODO, see https://git.autonomic.zone/coop-cloud/abra/src/branch/main/abra#L3-L55) but that is next to do. Then threading it somehow into some main and re-working our own parse_subcommand (deffo need help there). Working from https://github.com/andsens/docopt.sh. Really like that we just need to work with the docstring and then the python program updates parser in-place, super nice touch.

OK so current status is, you run `make docopt` and the Makefile will install the `docopt-sh` python package into a `.venv` and run it on the `abra` script. That then reads the `DOC` string and generates a parser blob that is updated in-place inside `abra`. I haven't quite finished writing the `DOC` (feel free if you get there! moved everything to the top and added a TODO, see https://git.autonomic.zone/coop-cloud/abra/src/branch/main/abra#L3-L55) but that is next to do. Then threading it somehow into some main and re-working our own `parse_subcommand` (deffo need help there). Working from https://github.com/andsens/docopt.sh. Really like that we just need to work with the docstring and then the python program updates parser in-place, super nice touch.
Author
Owner

OK, it is up!

https://git.autonomic.zone/coop-cloud/abra/src/branch/main/abra#L5-L42

@3wordchant if I haven't completely borked it all, could you maybe wire up the connection between the new abra function (see main at bottom) and threading arguments and so on down into the subcommands? https://github.com/andsens/docopt.sh#parser-output tells you how to interact with the parsed variables.

OK, it is up! https://git.autonomic.zone/coop-cloud/abra/src/branch/main/abra#L5-L42 @3wordchant if I haven't completely borked it all, could you maybe wire up the connection between the new `abra` function (see main at bottom) and threading arguments and so on down into the subcommands? https://github.com/andsens/docopt.sh#parser-output tells you how to interact with the parsed variables.
3wordchant was assigned by decentral1se 2020-10-26 10:43:16 +00:00
decentral1se self-assigned this 2020-10-26 10:43:18 +00:00
Owner

It's done

Parsing top-level arguments and commands works beautifully 🥰

Currently we still have (and are using) parse_subcommand, and manual argument parsing for the subcommands themselves. If there's a way of getting docopt.sh to parse subcommands' arguments, I am not finding it!

[It's done](https://git.autonomic.zone/coop-cloud/abra/src/branch/main/abra#L567-L591) Parsing top-level arguments and commands works beautifully 🥰 Currently we still have (and are using) [`parse_subcommand`](https://git.autonomic.zone/coop-cloud/abra/src/branch/main/abra#L189-L210), and [manual argument parsing for the subcommands themselves](https://git.autonomic.zone/coop-cloud/abra/src/branch/main/abra#L311-L313). If there's a way of getting docopt.sh to parse subcommands' arguments, I am not finding it!
Author
Owner

Yes! Rad! For the subcommands arguments I think the docopt method is to create a new file, with a new DOC="..." and generate the parser for that. Then in the main, pass the commands down into that script. That is the approach they take in https://github.com/docopt/docopt/tree/master/examples/git (python version). Not sure how fancy our subcommands want to get but the current setup seems fine? If at some point the subcommands get unruly, we could switch over to this? I'm easy, if you want, we can close this off for now.

Yes! Rad! For the subcommands arguments I think the docopt method is to create a new file, with a new DOC="..." and generate the parser for that. Then in the main, pass the commands down into that script. That is the approach they take in https://github.com/docopt/docopt/tree/master/examples/git (python version). Not sure how fancy our subcommands want to get but the current setup seems fine? If at some point the subcommands get unruly, we could switch over to this? I'm easy, if you want, we can close this off for now.
Owner

Belay my earlier remark!

Apparently works to just list all the subcommands at the top of the file with their args, it's not the neatest but I am Fine With It For Now.

Just making sure all the subcommands still work 👌

Belay my earlier remark! Apparently works to [just list all the subcommands at the top of the file with their args](https://git.autonomic.zone/coop-cloud/abra/src/branch/main/abra#L8-L22), it's not the neatest but I am Fine With It For Now. Just making sure all the subcommands still work 👌
Owner

Think everything is working now, including hidden commands (volume and stack).

Think everything is working now, including hidden commands (`volume` and `stack`).
This repo is archived. You cannot comment on issues.
No Milestone
2 Participants
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: coop-cloud/abra#18
No description provided.