-
Notifications
You must be signed in to change notification settings - Fork 47
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
Devex: Wrapper for node:utils parseArgs #5606
Conversation
… the parsing bits. also added some tests
…here were non-required positionals...it turns out parseArgs can handle those just fine. Also added some documentation in the ScriptConfig and ScriptParameter type definitions where appropriate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey Jim, this is great. In my mind it's a big improvement over the built-in parseArgs
because it allows for more config info (e.g. description) and makes the process for accessing values similar regardless if they are named or positional arguments.
I guess my only comment is that your excellent write-up of this is relegated to this pull-request. I know that we are relatively anti jsdoc here, but might be nice to capture the descriptions & examples from your PR into the reportUtils.ts
file.
…typescript shell scripts (that require the parsing of parameters) moving forward.
… description for future reference.
Great points! I have added more JSDoc to the types and I have also created a readme for the argument parser (which is basically just the content from this PR description, reworded to be less salesy). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍
Argument Parser for Shell Scripts Written in TypeScript
We have several scripts now that use node:utils
parseArgs
to parse command-line arguments, and it works great! Unfortunately, however, the scripts that use it have to implement a ton of code, to the extent that in some cases the code to configure and utilizeparseArgs
ends up being longer than the rest of the script combined.Here I've written a wrapper that we can call that does all of that work for us, and centralizes some of the text transformation stuff we were slapping on afterwards.
How to Use the Argument Parser in a Shell Script
Let's say you want the
$1
(first positional) argument to be required and you want to optionally support two additional arguments. Now you can just put this at the top of your script:And then elsewhere in the script you can get the values thusly:
In Action
Given the configuration above, say you call your script like so:
The argument parser will return the following:
Self-Documenting
All scripts that utilize the argument parser will get a
--help
flag that, when provided, will output usage information automatically generated from theScriptConfig
configuration object.Again, given the configuration above, say you call your script like so:
The argument parser will print the following: