-
Notifications
You must be signed in to change notification settings - Fork 126
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
Add argv parsing example #233
Conversation
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.
This is a pretty cool idea. I literally never thought to use Parsimmon for this.
If you don't mind addressing these comments I'd be happy to merge this in :)
examples/argv.js
Outdated
} | ||
}); | ||
|
||
let args = process.argv.slice(2).join(" "); |
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.
I think it might make more sense to take the entire options string as argv[2]
like node cli.js "--foo --qux=guz -b"
to show that this is parsing out one whole string and not relying on the shell (bash, etc) to parse for us.
examples/argv.js
Outdated
// one of possible quotes, then sequence of anything except that quote (unless escaped), then the same quote | ||
return P.oneOf(`"'`).chain(function(q) { | ||
return P.alt( | ||
P.regex(new RegExp(`[^\\\\${q}]+`)), |
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.
this regexp is pretty confusing to me. i think you might be able to rewrite this using some of the other parsimmon constructors?
you could also just write two separate string rules and then use alt() to combine them
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.
Yes, double escaping is going on here, not the cleanest code in the history :)
I agree with both points – will try to rewrite this as you suggested.
Can't find out, why Linter complains on this: option: function(r) {
return P.seq(
P.alt(
P.string("-").then(P.regex(/[a-z]/)),
P.string("--").then(r.word)
),
P.alt(
P.string("=").then(r.word),
P.of(true)
)
);
}, and wants me to change it to less readable this: option: function(r) {
return P.seq(
P.alt(P.string("-").then(P.regex(/[a-z]/)), P.string("--").then(r.word)),
P.alt(P.string("=").then(r.word),P.of(true))
);
}, To clarifyDirect run with
until I run |
The full command behind The problem is Prettier wants to reformat those lines. If I think if you use It might also be worth checking out if a newer version of Prettier formats that nicer. |
I checked and unfortunately no.
Tried this:
and also no. It wants me to fold an array into a single line... I personally never use such tools – the're always too stupid and unable to pick normal cases. |
Looks great now. Thanks for the contribution @ivan-kleshnin! |
No problem. Thank you for such a great library! |
Hey guys, can I suggest an example of argv (CLI) parsing?
1) NodeJS provides
process.argv
obviously but it's quite low-level. For example, I'd like to haveparsed as
instead of
You may also want to parse
argv
-like strings in Browser (to highlight code or whatever).2) Moreover, this example showcases a parsing of quoted strings:
with escape support, which no other example currently does (except
json.js
but not 100% close).Any remarks are welcome. I followed the style of other examples – it passes
$ npm run lint
.