-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Implement command line parser builtin #613
Comments
Great idea! In the future we could extend these library functions with structs:
|
My proposed usage code may look familiar; I modelled it after the Python |
Oh I see @hdwalters then we can stick to it |
Would this be better implemented as a library that gets solidified before stdlib adoption? I'm interested in flag support. |
Hi Adam, thanks for the feedback!
I'm not sure what you mean by this; are you describing (i) the standard library (written in pure Amber) or (ii) the wider ecosystem, including builtins? If you're suggesting we should write the parser in pure Amber, that was never the plan. And having spent this week writing Rust code, I really don't think it would be possible to do it that way, and get anywhere close to the functionality I want. This is for several reasons:
What are you trying to acheive? Perhaps additional functionality can be included in the builtins. Though I should point out that this is already supported by the Bash
|
I'm looking at trying out amber in a situation where we'd use the script(s) on mac and linux machines (mac host pushing script/commands to bash on linux). Amber caught my eye because it simplifies bash programming. The script would have a couple flags/arguments. Coming from Go the parsing functions ( |
Ok, thanks for elaborating. I'm unfamiliar with the Go development cycle, but from your description, this won't really work in this case. We can add functionality on top of the core compiler in one of two ways:
For reasons given above, we cannot implement the parser in pure Amber, and therefore have no choice but to compile it into the binary as a set of builtins. |
Is your feature request related to a problem? Please describe.
When writing Bash scripts taking command line options, I use
getopt
to preprocess the command line, and awhile
loop andcase
statement to handle individual options:This can handle:
--label=zzz
or--label "zzz"
-lzzz
--flag
-f
-xyz
expanded to-x -y -z
while
loopDescribe the solution you'd like
However, this is such a painful process, I have to copy and adapt a previous script every time. I would like to avoid this pain in Amber, by writing a set of builtin functions. This is my proposal:
The
parser
builtin creates a parser struct to hold the array of (reference counted) argument structs for Bash generation. Theparam
builtin parses the option match string, a default value (which also defines whether it takes a parameter at all, and the type of that parameter) and a help string, and creates the argument structs for theparser
. Thegetopt
builtin causes the command line to be parsed. Subsequently, the argument structs are responsible for returning the parsed values for use in the main script.Describe alternatives you've considered
The only alternative would be to attempt to write this as a standard library function, but (i) it requires interaction between the various components, which is just not possible in pure Amber, and (ii) it would be unable to support typed arguments, which are necessary to (for example) iterate over array arguments.
Additional context
N/A
The text was updated successfully, but these errors were encountered: