Integrate getopt for CLI parsing #18
Labels
No Label
breaking-change
bug
CI/CD
design
documentation
duplicate
enhancement
help wanted
invalid
plugin
question
secrets
shell-completion
versioning
wontfix
No Milestone
No Assignees
2 Participants
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: coop-cloud/abra#18
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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?👍👍👍 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?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.
Integrating with a CLI parser so as to avoid long-term foot gunsto Integrate getopt for CLI parsinghttp://docopt.org/ maybe?
Bash implementation: https://github.com/docopt/docopts
Python-to-bash: https://github.com/andsens/docopt.sh
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.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 binarypython3
and make our lives familiar and easy, but I am still feeling OK about the Bash direction with or withoutdocopts
.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.
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!
OK so current status is, you run
make docopt
and the Makefile will install thedocopt-sh
python package into a.venv
and run it on theabra
script. That then reads theDOC
string and generates a parser blob that is updated in-place insideabra
. I haven't quite finished writing theDOC
(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 ownparse_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, 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.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!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.
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 👌
Think everything is working now, including hidden commands (
volume
andstack
).