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

Feature: pre-parse string of arguments to argv #221

Closed
wants to merge 3 commits into from

Conversation

juergba
Copy link
Contributor

@juergba juergba commented Nov 12, 2019

Description

closes #180

Pre-parse a string of arguments into argv, as Node launched in Bash builds process.argv.slice(2).
Incl. the handling of quoting and escaped metacharacters.

We add a new method toArgv()to Parser.

'use strict';
const parser = require('yargs-parser');

let argv = parser.toArgv(String.raw`--foo "bar hello" "testing \" this"`);
console.log(argv);       // [ '--foo', 'bar hello', 'testing " this' ]

let args = parser(argv, {
        array: ['foo']
});
console.log(args);       // { _: [], foo: [ 'bar hello', 'testing " this' ] }

Running Node on Bash and calling parser.toArgv should result in an identical argv:

$ node test.js --foo "bar hello" "testing \" this"
// process.argv.slice(2) === [ '--foo', 'bar hello', 'testing " this' ]
// let res = parser.toArgv(`--foo "bar hello" "testing \\" this"`)  // ok, works
let res = parser.toArgv(String.raw`--foo "bar hello" "testing \" this"`)
// res === [ '--foo', 'bar hello', 'testing " this' ]

@juergba juergba marked this pull request as ready for review November 14, 2019 14:06
@juergba juergba changed the title Feature: bash pre-parse string of arguments to argv Feature: pre-parse string of arguments to argv Nov 14, 2019
@bcoe
Copy link
Member

bcoe commented Nov 16, 2019

🎉 thanks for taking this on @juergba, @cdhowie do you think this would work for your use-case.

@bcoe
Copy link
Member

bcoe commented Feb 9, 2020

@juergba coming back to this, I would personally rather we just document arrgv in the README, with a clear example of how one would use this to parse a complex argument string, rather than introducing an additional dependency to the chain.

At the end of the day, someone just invokes aargv, might as well just point folks towards that dependency?

@juergba
Copy link
Contributor Author

juergba commented Feb 9, 2020

@bcoe yes, I can live with your decision.

I leave the README update to someone else though, I don't have time for this right now.
So I'm closing this PR.

@juergba juergba closed this Feb 9, 2020
@juergba juergba deleted the issue/bash-parse branch February 9, 2020 14:14
@bcoe
Copy link
Member

bcoe commented Feb 9, 2020

@juergba 👌 appreciate the work you put into this.

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.

Quotes not consumed
2 participants