Skip to content

Instantly share code, notes, and snippets.

@buu700
Created October 2, 2020 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buu700/72d0461d318bfe8c11e36d2316882b2a to your computer and use it in GitHub Desktop.
Save buu700/72d0461d318bfe8c11e36d2316882b2a to your computer and use it in GitHub Desktop.
argbash function
parseArgs () {
argbash-init "${@}" - |
argbash - |
tr '\n' '☁' |
perl -pe 's/.*START OF CODE GENERATED BY Argbash(.*)END OF CODE GENERATED BY Argbash.*/#$1/g' |
tr '☁' '\n'
}
@matejak
Copy link

matejak commented Oct 2, 2020

And here comes the curious Argbash developer :-)
Your code looks very interesting, what is this supposed to do? Strip the template part of the script and the non-essential generated stuff?

@buu700
Copy link
Author

buu700 commented Oct 2, 2020

lol, hey, how'd you find this? There's some more context in my comment on the Hacker News thread, but this is a snippet taken from github.com/cyph/cyph that we use for calling Argbash dynamically rather than requiring a bunch of boilerplate in the script itself.

The regex replacement must have been needed to prevent it from failing with an error that START is not a command, or something to that effect.

I think it would actually be really useful if this were an official feature built in to Argbash (ideally with another flag to modify the help text). I'm sure plenty of scripts benefit from having the whole generated output that they can then drop in and modify by hand, but I'd imagine the more common case is just wanting to start accepting some arguments with minimal effort. This would also allow for a simpler hello world example.

@matejak
Copy link

matejak commented Oct 4, 2020

I came here from Hacker News, and I don't get what exactly is the problem. You mention that there is "too verbose output", but what does that mean?
There are argument-parsing libraries (e.g. docopt in bash) that you can use to get parsing done by third party in a small number of lines.
Then, you can have Argbash to generate a parsing snippet and just source it in your script.
Finally, how would you like an Argbash quasi-interpreter?

Anyway, if you are thinking of something entirely else, please create an issue in the project where you describe it in greater detail.

@buu700
Copy link
Author

buu700 commented Oct 4, 2020

Good to know about docopts, thanks! That looks like it may be closer to what I wanted.

As in the Hacker News example, I just wanted a simple way to add some parameters to existing scripts and easily maintain them. eval "$(parseArgs --opt foo)" is easy to remember without referencing documentation and it's obvious at a glance exactly what it does.

Maybe I misunderstood the documentation, but it seems like with argbash, if I don't do this, my options are to either paste in the generated output (and just re-run the commands every time I need to change it) or change the script to a .m4 file and compile it to a .sh every time I run it. The latter option wouldn't be too bad, but for what I need it seems like a bit more work to accomplish essentially the same thing.

The Argbash quasi-interpreter link goes to docopts (not sure if that was intentional), but is the idea that it would let me keep my .sh extension and just point the shebang at argbash instead of bash? If so, that sounds great — seems to have all the same benefits as my setup, but neatly addresses the limitations in the Argbash CLI vs the declarative syntax.

@matejak
Copy link

matejak commented Oct 5, 2020

The Argbash quasi-interpreter link goes to docopts ...

I have fixed the link. It leads to an Argbash issues that links to the prototype implementation.

I think that I am slowly starting to see your point - you don't like the generated code in your script.
Therefore, I think that the "interpreter" is the best option for you - instead of calling that function, you could declare the interface in a standard Argbash way, i.e. using comments, which is more descriptive, and you get support for additional goodies s.a. short options, default values, help and manpage output, while the "interpreter" will add the generated code for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment