Hacker News new | past | comments | ask | show | jobs | submit login

This one seems way better

It didn't account for the presence of a '--' to end the parsing of named arguments but that's it






> It didn't account for the presence of a '--' to end the parsing of named arguments but that's it

That’s just something getopt does and some programs adopted. If you asked me to write a parser, I wouldn’t necessarily include that either if you didn’t ask for it.


If you don't include it you can't have positional arguments that look like options

Some positional arguments can be filenames, filenames can be --help and --verbose or --name=Frank

You have to have `--` or something similar to have a correct program


> You have to have `--` or something similar to have a correct program

No, only if the positional arguments need to support arbitrary strings. If you have something like a package manager and the first positional argument is the subcommand and everything after is an alphanumeric package name, you don’t need to support the double dash.


But also like as a matter of principle it's annoying when you're working on a big codebase, and you have weird, hidden, silently explosive unwritten contracts between random components

Package managers is an especially bad example because github projects typically are github.com/author-name/words-separated-by-dashes

So you will probably have somebody along the way pester you about allowing dashes between words, to play nice with github

But who's to know if the guy making that change will think of disallowing dashes at the start of the words? Likely he'll just add \- to /[A-Z\-]+/

Suddenly the script you wrote starts getting passed positional arguments that have dashes in them, until some wise guy tries to create a package called `--verbose`, then notices unintended effects on your pages, goes ahead trying `--verbose -- react`, ...

So anyway -- if I'm making a heavily reusable piece of code like this I make it as general purpose as possible in a way that makes it impossible to mis-use it


You don't know what it's going to be used for when you get that prompt. Not handling arbitrary strings is a bug



Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: