vercel / arg

Simple argument parsing
https://npmjs.com/arg
MIT License
1.23k stars 54 forks source link

Ability to "stop early" #31

Closed blakeembrey closed 5 years ago

blakeembrey commented 5 years ago

In some projects, I need the ability to "stop early" when parsing the arguments. I want it to parse up until the first _ argument and then break. This is useful for projects such as https://github.com/TypeStrong/ts-node (like node.js core), where the arguments before the script file are part of the execution and after the script name part of the script arguments.

blakeembrey commented 5 years ago

This may actually be related to https://github.com/zeit/arg/issues/6, since stopping early would kind of solve that use-case (assuming it's entirely ordered)

rauchg commented 5 years ago

I believe we did solve that with permissive: true. However, @Qix- and I discussed this one for a while. If one wanted to truly stop parsing early, we need to ditch the ƒ(spec, args) -> Object signature in favor of returning a generator that emits parsed tokens.

rauchg commented 5 years ago

The conclusion at that point was that generators would indeed be the perfect base abstraction, and that one could then 'objectify' it, but that in general it would just add lots of complexity for a fairly niche use-case.

blakeembrey commented 5 years ago

@rauchg Interesting that --permissive solved the use-case, unfortunately it wouldn't solve mine since I need to stop immediately. I created a sample PR and it seems pretty simple to support so maybe we slightly different cases in mind. How do you separate possible conflicts between the root command and sub-commands with permissive (or is it just ignored)?

Another way to solve this could be to allow { _: Handler } and support returning a symbol to break parsing, but I opted to keep it simple with just a stopEarly: true option that stops once you add an argument to result._.

Qix- commented 5 years ago

See https://github.com/zeit/arg/issues/6#issuecomment-450000740 - does that sound about right?

blakeembrey commented 5 years ago

@Qix- Not quite sure what you're referring to, but do you want me to call it stopAtPositional instead of stopEarly? I just used stopEarly since it's what minimist also used, but I'm not tied to either way. I do need this feature, however, for projects like ts-node to work.

Qix- commented 5 years ago

stopEarly is fine I suppose but it doesn't really indicate what "early" means. If I see stopEarly in someone's code without knowing what arg is, I'll have to look up what it means. stopAtPositional tells me everything I need to know about the behavior therein.

blakeembrey commented 5 years ago

@Qix- Great, I'll update the PR 👍

Qix- commented 5 years ago

Released as 4.1.0.