Example #1
0
  it("should not drop down with empty args (without positional arguments)", function () {
    parser = new ArgumentParser({debug: true});
    parser.addArgument(['-f', '--foo']);

    assert.doesNotThrow(function () { parser.parseArgs([]); });
  });
Example #2
0
 assert.throws(function () {
   parser.parseArgs('--bar bar'.split(' '));
 });
Example #3
0
 assert.doesNotThrow(function () {
   parser.parseArgs('--bar 1'.split(' '));
 });
Example #4
0
  it('test options that may or may not be arguments', function () {
    parser = new ArgumentParser({debug: true});
    parser.addArgument([ '-x' ], { type: 'float' });
    parser.addArgument([ '-3' ], { dest: 'y', type: 'float' });
    parser.addArgument([ 'z' ], { nargs: '*' });

    args = parser.parseArgs([]);
    assert.deepEqual(args, { y: null, x: null, z: [] });
    args = parser.parseArgs([ '-x', '2.5' ]);
    assert.deepEqual(args, { y: null, x: 2.5, z: [] });
    args = parser.parseArgs([ '-x', '2.5', 'a' ]);
    assert.deepEqual(args, { y: null, x: 2.5, z: [ 'a' ] });
    args = parser.parseArgs([ '-3.5' ]);
    assert.deepEqual(args, { y: 0.5, x: null, z: [] });
    args = parser.parseArgs([ '-3-.5' ]);
    assert.deepEqual(args, { y: -0.5, x: null, z: [] });
    args = parser.parseArgs([ '-3', '.5' ]);
    assert.deepEqual(args, { y: 0.5, x: null, z: [] });
    args = parser.parseArgs([ 'a', '-3.5' ]);
    assert.deepEqual(args, { y: 0.5, x: null, z: [ 'a' ] });
    args = parser.parseArgs([ 'a' ]);
    assert.deepEqual(args, { y: null, x: null, z: [ 'a' ] });
    args = parser.parseArgs([ 'a', '-x', '1' ]);
    assert.deepEqual(args, { y: null, x: 1, z: [ 'a' ] });
    args = parser.parseArgs([ '-x', '1', 'a' ]);
    assert.deepEqual(args, { y: null, x: 1, z: [ 'a' ] });
    args = parser.parseArgs([ '-3', '1', 'a' ]);
    assert.deepEqual(args, { y: 1, x: null, z: [ 'a' ] });

    assert.throws(function () {
      args = parser.parseArgs([ '-x' ]);
    });
    assert.throws(function () {
      args = parser.parseArgs([ '-y2.5' ]);
    });
    assert.throws(function () {
      args = parser.parseArgs([ '-xa' ]);
    });
    assert.throws(function () {
      args = parser.parseArgs([ '-x', '-a' ]);
    });
    assert.throws(function () {
      args = parser.parseArgs([ '-x', '-3' ]);
    });
    assert.throws(function () {
      args = parser.parseArgs([ '-x', '-3.5' ]);
    });
    assert.throws(function () {
      args = parser.parseArgs([ '-3', '-3.5' ]);
    });
    assert.throws(function () {
      args = parser.parseArgs([ '-x', '-2.5' ]);
    });
    assert.throws(function () {
      args = parser.parseArgs([ '-x', '-2.5', 'a' ]);
    });
    assert.throws(function () {
      args = parser.parseArgs([ '-3', '-.5' ]);
    });
    assert.throws(function () {
      args = parser.parseArgs([ 'a', 'x', '-1' ]);
    });
    assert.throws(function () {
      args = parser.parseArgs([ '-x', '-1', 'a' ]);
    });
    assert.throws(function () {
      args = parser.parseArgs([ '-3', '-1', 'a' ]);
    });
  });
Example #5
0
  it('RawTextHelpFormatter', function () {
    parser = new argparse.ArgumentParser({
      debug: true,
      prog: 'PROG',
      formatterClass: argparse.RawTextHelpFormatter,
      description: 'Keep the formatting\n' +
                   '    exactly as it is written\n' +
                   '\n' +
                   'here\n'
    });

    parser.addArgument(['--baz'], {
      help: '    baz help should also\n' +
            'appear as given here'
    });

    a = parser.addArgument(['--foo'], {
      help: '  foo help should also\n' +
            'appear as given here'
    });

    parser.addArgument(['spam'], {
      'help': 'spam help'
    });

    group = parser.addArgumentGroup({
      title: 'title',
      description: '    This text\n' +
                   '  should be indented\n' +
                   '    exactly like it is here\n'
    });

    group.addArgument(['--bar'], {
      help: 'bar help'
    });

    helptext = parser.formatHelp();
    // test selected clips
    assert(helptext.match(parser.description));
    // part of the unchanged argument help, with spaces
    assert(helptext.match(/( {14})appear as given here/gm));

/*
usage: PROG [-h] [--foo FOO] [--bar BAR] spam

Keep the formatting
    exactly as it is written

here

positional arguments:
  spam        spam help

optional arguments:
  -h, --help  show this help message and exit
  --foo FOO       foo help should also
              appear as given here

title:
      This text
    should be indented
      exactly like it is here

  --bar BAR   bar help
*/
  });
Example #6
0
  it('test specifying an Optional arg for an Optional', function () {
    parser = new ArgumentParser({debug: true});
    parser.addArgument([ '-w' ], { nargs: '?' });
    parser.addArgument([ '-x' ], { const: 42, nargs: '?', constant: 42 });
    parser.addArgument([ '-y' ], { default: 'spam', nargs: '?', defaultValue: 'spam' });
    parser.addArgument([ '-z' ], {
      default: '84',
      nargs: '?',
      type: 'int',
      const: '42',
      defaultValue: '84',
      constant: '42'
    });

    args = parser.parseArgs([]);
    assert.deepEqual(args, { y: 'spam', x: null, z: 84, w: null });
    args = parser.parseArgs([ '-w' ]);
    assert.deepEqual(args, { y: 'spam', x: null, z: 84, w: null });
    args = parser.parseArgs([ '-w', '2' ]);
    assert.deepEqual(args, { y: 'spam', x: null, z: 84, w: '2' });
    args = parser.parseArgs([ '-x' ]);
    assert.deepEqual(args, { y: 'spam', x: 42, z: 84, w: null });
    args = parser.parseArgs([ '-x', '2' ]);
    assert.deepEqual(args, { y: 'spam', x: '2', z: 84, w: null });
    args = parser.parseArgs([ '-y' ]);
    assert.deepEqual(args, { y: null, x: null, z: 84, w: null });
    args = parser.parseArgs([ '-y', '2' ]);
    assert.deepEqual(args, { y: '2', x: null, z: 84, w: null });
    args = parser.parseArgs([ '-z' ]);
    assert.deepEqual(args, { y: 'spam', x: null, z: 42, w: null });
    args = parser.parseArgs([ '-z', '2' ]);
    assert.deepEqual(args, { y: 'spam', x: null, z: 2, w: null });

    assert.throws(function () {
      args = parser.parseArgs([ '2' ]);
    });
  });
Example #7
0
 assert.throws(function () {
   args = parser.parseArgs([ '-y' ]);
 });
Example #8
0
 function () { parser.addArgument(['-f']); },
Example #9
0
 function () { parser.addArgument(['-f', '--flame']); },
Example #10
0
 function () { parser.parseArgs(['-d', 'abc']); },
Example #11
0
 function () { parser.parseArgs(['-d', '13/1/2000']); },
Example #12
0
 function () { parser.parseArgs(['--eggs', 'a']); },
Example #13
0
 beforeEach(function () {
   parser = new ArgumentParser({debug: true, fromfilePrefixChars: '@'});
   parser.addArgument(['-a']);
   parser.addArgument(['x']);
   parser.addArgument(['y'], {nargs: '+'});
 });
Example #14
0
  it('RawDescriptionHelpFormatter', function () {

    parser = new argparse.ArgumentParser({
      debug: true,
      prog: 'PROG',
      formatterClass: argparse.RawDescriptionHelpFormatter,
      description: 'Keep the formatting\n' +
                   '    exactly as it is written\n' +
                   '\n' +
                   'here\n'
    });

    a = parser.addArgument(['--foo'], {
      help: '  foo help should not\n' +
            '    retain this odd formatting'
    });

    parser.addArgument(['spam'], {
      'help': 'spam help'
    });

    group = parser.addArgumentGroup({
      title: 'title',
      description: '    This text\n' +
                   '  should be indented\n' +
                   '    exactly like it is here\n'
    });

    group.addArgument(['--bar'], {
      help: 'bar help'
    });

    helptext = parser.formatHelp();
    // test selected clips
    // the parser description is not changed
    assert(helptext.match(parser.description));
    // the argument help is changed
    assert.equal(helptext.match(a.help), null);
    // the trimmed argument help matches
    assert(helptext.match(/foo help should not retain this odd formatting/));

/*
usage: PROG [-h] [--foo FOO] [--bar BAR] spam

Keep the formatting
    exactly as it is written

here

positional arguments:
  spam        spam help

optional arguments:
  -h, --help  show this help message and exit
  --foo FOO   foo help should not retain this odd formatting

title:
      This text
    should be indented
      exactly like it is here

  --bar BAR   bar help
*/
  });
Example #15
0
 assert.doesNotThrow(function () { parser.parseArgs([]); });
Example #16
0
 function () { parser.addArgument(['-m', '--foo']); },
Example #17
0
 function ()  {parser.parseArgs([]); },
Example #18
0
 function () {
   parser.parseArgs(['-2']);
 },
Example #19
0
 assert.throws(function () {
   args = parser.parseArgs([ 'a', 'b', 'c', 'd' ]);
 });
Example #20
0
 function () {
   parser.parseArgs(['-1', '-1']);
 },
Example #21
0
 assert.throws(function () {
   args = parser.parseArgs([ '--baz', 'a', 'b' ]);
 });
Example #22
0
  it('test empty and space containing arguments', function () {
    parser = new ArgumentParser({debug: true});
    parser.addArgument([ 'x' ], { nargs: '?' });
    parser.addArgument([ '-y', '--yyy' ], { dest: 'y' });

    args = parser.parseArgs([ '' ]);
    assert.deepEqual(args, { y: null, x: '' });
    args = parser.parseArgs([ 'a badger' ]);
    assert.deepEqual(args, { y: null, x: 'a badger' });
    args = parser.parseArgs([ '-a badger' ]);
    assert.deepEqual(args, { y: null, x: '-a badger' });
    args = parser.parseArgs([ '-y', '' ]);
    assert.deepEqual(args, { y: '', x: null });
    args = parser.parseArgs([ '-y', 'a badger' ]);
    assert.deepEqual(args, { y: 'a badger', x: null });
    args = parser.parseArgs([ '-y', '-a badger' ]);
    assert.deepEqual(args, { y: '-a badger', x: null });
    args = parser.parseArgs([ '--yyy=a badger' ]);
    assert.deepEqual(args, { y: 'a badger', x: null });
    args = parser.parseArgs([ '--yyy=-a badger' ]);
    assert.deepEqual(args, { y: '-a badger', x: null });

    assert.throws(function () {
      args = parser.parseArgs([ '-y' ]);
    });
  });
Example #23
0
 assert.throws(function () {
   args = parser.parseArgs([ 'a', '-c' ]);
 });
Example #24
0
  it('ArgumentDefaultsHelpFormatter', function () {

    parser = new argparse.ArgumentParser({
      debug: true,
      formatterClass: argparse.ArgumentDefaultsHelpFormatter,
      description: 'description'
    });

    parser.addArgument(['--foo'], {
      help: 'foo help - oh and by the way, %(defaultValue)s'
    });

    parser.addArgument(['--bar'], {
      action: 'storeTrue',
      help: 'bar help'
    });

    parser.addArgument(['spam'], {
      help: 'spam help'
    });

    parser.addArgument(['badger'], {
      nargs: '?',
      defaultValue: 'wooden',
      help: 'badger help'
    });

    group = parser.addArgumentGroup({
      title: 'title',
      description: 'group description'
    });

    group.addArgument(['--baz'], {
      type: 'int',
      defaultValue: 42,
      help: 'baz help'
    });

    helptext = parser.formatHelp();
    // test selected clips
    // test_argparse.py can match the whole help
    assert(helptext.match(/badger help \(default: wooden\)/));
    assert(helptext.match(/foo help - oh and by the way, null/));
    assert(helptext.match(/bar help \(default: false\)/));
    assert(helptext.match(/title:\n {2}group description/)); // test indent
    assert(helptext.match(/baz help \(default: 42\)/im));

/*
usage: PROG [-h] [--foo FOO] [--bar] [--baz BAZ] spam [badger]

description

positional arguments:
  spam        spam help
  badger      badger help (default: wooden)

optional arguments:
  -h, --help  show this help message and exit
  --foo FOO   foo help - oh and by the way, null
  --bar       bar help (default: false)

title:
  group description

  --baz BAZ   baz help (default: 42)
*/
  });