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

named and required positional arguments? #27

Open
greggman opened this issue Jul 6, 2018 · 1 comment
Open

named and required positional arguments? #27

greggman opened this issue Jul 6, 2018 · 1 comment

Comments

@greggman
Copy link

greggman commented Jul 6, 2018

Maybe I missed it but is it possible to define positional required arguments?

Let's say I wanted to make the cp command in the format

  cp [options] src dst

Is it possible to get optionator to fail if there are not exactly 2 positional arguments? And is it possible to get the first one added as src and the 2nd as dst.

I find I create lots of commands like this and I end up with lots of boilerplate like

const args = optionator.parse(process.argv);
if (args._.length < 1) {
   console.log("missing src filename");
    printHelp();
    return;
}
if (args._.length < 2) {
   console.log("missing dst filename");
   printHelp();
   return;
}
if (args._.length > 2) {
  console.log("unknown 3rd argument");
  printHelp();
  return;
}
const src = args._[0];
const dst = args._[1];

that just seems like something optionator could handle saving me all the work. Does it already do that and I'm just missing it?

What I'd like to write is

const optionSpec = {
  options: [
    { option: 'help', alias: 'h', type: 'Boolean', description: 'displays help' },
    { option: 'recursive, alias: 'r', type: 'Boolean', description: 'copy subfolders', },
    { option: 'src', type: 'String', description: 'source filespec', required: true, positional: true, },
    { option: 'dst', type: 'String', description: 'destination filespec', required: true, positional: true, },
  ],
  prepend: 'cp [options] src dst',
};

where positional: true means it comes from positional arguments and based on the order in the optins array one comes before the other. Then'd use it

const args = optionator.parse(process.argv);
const src = args.src;
const dst = args.dst;

And it would fail if src and dst were not provided

@gkz
Copy link
Owner

gkz commented Jul 11, 2018

Interesting, no it doesn't support this type of thing, but would be nice to have

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

No branches or pull requests

2 participants