dthree / vorpal

Node's framework for interactive CLIs
http://vorpal.js.org
MIT License
5.64k stars 280 forks source link

Negative numbers as arguments don't work #117

Closed omnidan closed 8 years ago

omnidan commented 8 years ago

They are parsed as options, is there any way to make it parse the argument as a number instead? (and ignore options completely, at least when none are defined)

$ test -100

  Missing required argument. Showing Help:

  Usage: test [options] <points>

This is probably related to #8.

dthree commented 8 years ago

About to do a number on arg parsing, will take this into account.

omnidan commented 8 years ago

@dthree cool, thanks! BTW: being able to disable options for certain commands would solve the problem for me.

dthree commented 8 years ago

Making progress on this issue, btw.


being able to disable options for certain commands would solve the problem for me.

Could you please clarify what you mean by that?

dthree commented 8 years ago

Okay @omnidan , this is a bit harder than I thought.

https://github.com/substack/node-optimist/issues/79

It's hard to implement something that doesn't break something else.


Do you have any ideas on how to possibly implement this?

dthree commented 8 years ago

@omnidan not sure what I'm going to do about this, as pretty much every Node CLI app out there has this problem. If you have a bright idea, open a PR and we'll discuss it!

omnidan commented 8 years ago

@dthree my idea was adding a way to disable options:

vorpal
  .command('num')
  .noOptions() // my proposed feature
  .action(function (args, cb) {
    this.log(args) // `num -1` works with negative numbers because options aren't parsed!
    cb()
  })
dthree commented 8 years ago

Hmm..... I can do this, but I hate adding API features for edge cases, as that's what makes a library look crufty and overwhelming.

How about this: If this comes up with others as well, I'll take it up. A workaround for now that you can apply is either:

// Take quotes
vorpal$ num "-1"
vorpal.command('num')
  .parse(function (str) {
    // modify the command, looking for negative numbers and 
    // wrapping them in double quotes, or something.
    return str;
  }).action(action);

Does this help?

omnidan commented 8 years ago

@dthree Could I use parser() to define a completely custom parser function (that ignores options), e.g.:

vorpal.command('num')
  .parse(function (n) {
    this.log(n) // `num -1` should print `-1` now
  })
dthree commented 8 years ago

Yes, but play with its sytnax.

dreadjr commented 7 years ago

In my database all the id's have hyphens in them. I was able to get around the logic with the following, not pretty but works.

const vorpal = require('vorpal')();
vorpal
  .command('lookup')
  .option('-i, --id <id>', 'id')
  .option('-d, --default [val]')
  .action((args) => {
    console.log(args);
    return Promise.resolve(args);
  });

vorpal
  .delimiter("test >")
  .show()
  .parse(process.argv);

/*
console.log(vorpal.execSync('lookup --id -abc'));
// > Missing required value for option --id

console.log(vorpal.execSync('lookup --id "-abc"'));
// > Missing required value for option --id

console.log(vorpal.execSync('lookup --id-abc'));
// > Invalid option: 'id-abc'

console.log(vorpal.execSync('lookup --id=-abc'));
// > { options: {} }

console.log(vorpal.execSync('lookup --id="-abc"'));
// > { options: {} }

console.log(vorpal.execSync('lookup -i"-abc"'));
// > { options: { id: '-abc' } }

console.log(vorpal.execSync('lookup -i-abc'));
// > node test.js lookup -i-abc
// > { options: { id: '-abc' } }
*/

The last one is what i have been going with for now. node test.js lookup -i-abc

Maybe i will look into single and/or tilda quoted option values using a custom parser.

cnaccio commented 4 years ago

Ran into this issue as well, and the following worked for me using the parse api.

vorpal
    .command('config set <setting> <value>')
    .description('Configure various default settings')
    .parse((command, args) => {
        return command.replace('-', '++'); // Handle negative numbers
    })
    .action(async (args, callback) => {

        // Handle negative numbers
        if (typeof args.value == 'string' && args.value.includes('++')) {
            args.value = args.value.replace('++', '') * -1;
        }

        // Attempt to get the config value
        const value = db.get(args.setting).value();
        if (value != undefined) {
            db.set(args.setting, args.value).write();
            vorpal.activeCommand.log(
                chalk.greenBright`Successfully set ${chalk.bold(args.setting)} to ${chalk.bold(args.value)}`
            );
        } else {
            vorpal.activeCommand.log(
                chalk.yellow`${chalk.bold(args.setting)} is not a valid config setting.`
            );
        }

        // Return back to CLI
        callback();
    });