Skip to content
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 halt-at-non-option configuration option #130

Merged
merged 2 commits into from
Nov 9, 2018

Conversation

wmertens
Copy link
Contributor

@wmertens wmertens mentioned this pull request Aug 18, 2018
@wmertens
Copy link
Contributor Author

wmertens commented Sep 5, 2018

@bcoe any chance of a quick merge? It's fully downwards-compatible…

@bcoe
Copy link
Member

bcoe commented Oct 6, 2018

@wmertens @Qard this feature feels a little bit magical to me. It's not really stopping on an unknown argument, it's essentially stopping parsing as soon as a positional argument is hit rather than a flag argument right?

In the cases where I've needed to split command line arguments (usually because one bin is instrumenting another bin) I've used a helper like this:

https://github.com/bcoe/c8/blob/master/lib/parse-args.js#L60

I wonder if instead of adding this additional parsing option, we could find a way to formalize this helper in yargs? I think this is the use-case we're all trying to solve.

I'm swamped with open-source projects these days, so don't get to all my GitHub issues, perhaps I could convince you both to join me in slack here:

http://devtoolscommunity.herokuapp.com/

☝️ we've been using this slack to discuss JavaScript tooling related issues.

@wmertens wmertens changed the title Add stop-at-unknown configuration option Add halt-at-non-option configuration option Oct 9, 2018
@spion
Copy link

spion commented Oct 11, 2018

FYI I use the following workaround to stop parsing at the first positional argument:

function parseToPositional(yargs: yargs.Argv, cmd: string[]) {
  let newCmd = cmd.map((c, i) => (c.startsWith('-') ? c : c + ':' + i.toString()))
  let positional = yargs.parse(newCmd)
  if (!positional._.length) return yargs.parse(cmd)

  let position = Number(positional._[0].substr(positional._[0].lastIndexOf(':') + 1))

  let result = yargs.parse(cmd.slice(0, position))
  result._ = result._.concat(cmd.slice(position))
  return result
}

Appending ':index' to each of the non-switch segments ensures that the boolean switches are correctly interpreted and that we're not finding an argument to a switch instead of a positional argument - e.g. cmd --arg positional --other other positional should not find the first positional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants