Example #1
0
    def test_positional_args(self):
        parser = Parser('', '',
            Arg(('-a', '--arg'), 'arg'))

        _, args = parser.parse_args('-a asdf'.split())
        self.assertEqual(args.ns.arg, 'asdf')

        _, args = parser.parse_args('--arg asdf'.split())
        self.assertEqual(args.ns.arg, 'asdf')
Example #2
0
    def test_argparse_example(self):
        p = Parser(
            'cmd', 'Process some integers',
            Arg('integers', 'an integer for the accumulator',
                metavar='N', type=int, nargs='+'),
            Arg('--sum', 'sum the integers (default: find the max)',
                dest='accumulate', action='store_const',
                const=sum, default=max))
        parser, args = p.parse_args('1 2 3 4'.split())
        self.assertEqual(args.ns.accumulate(args.ns.integers), 4)

        parser, args = p.parse_args('1 2 3 4 --sum'.split())
        self.assertEqual(args.ns.accumulate(args.ns.integers), 10)
Example #3
0
    def test_lazy(self):
        p = Parser('p', 'description',
                Arg('--b', 'description', types.Int.positive,
                    Lazy(lambda ns: ns.a)),
                Arg('a', 'description', types.Int.positive))

        parser, args = p.parse_args('1234'.split())
        self.assertEqual(args.ns.a, 1234)
        self.assertEqual(args.ns.b, 1234)

        parser, args = p.parse_args('--b 4321 1234'.split())
        self.assertEqual(args.ns.a, 1234)
        self.assertEqual(args.ns.b, 4321)
Example #4
0
 def test_group(self):
     p = Parser('p', 'p-description',
         Group('grp', 'grp-description',
             Arg('a'),
             Arg('b')))
     parse, args = p.parse_args('a b'.split())
     self.assertEqual(args.ns.a, 'a')
     self.assertEqual(args.ns.b, 'b')
Example #5
0
    def test_to_named_params(self):
        parser = Parser('name', 'description',
            Arg('a', 'arg a'),
            Arg('b', 'arg b'),
            Arg('c', 'arg c'))

        _, args = parser.parse_args('a b c'.split())
        self.assertEqual(args.ns.a, 'a')
        self.assertEqual(args.ns.b, 'b')
        self.assertEqual(args.ns.c, 'c')
Example #6
0
    def test_subparser_common_args(self):
        p = Parser(
            'cmd', 'cmd-description',
            SubParsers('sp', 'sp-description', KW(dest='sp'),
                Arg('arg0', 'arg0-description'),
                Parser('p0', 'p0-description'),
                Parser('p1', 'p0-description',
                    Arg('p1-arg0', 'p1-arg0-description')),
                Arg('arg1', 'arg0-description')))

        parser, args = p.parse_args('p0 1 2'.split())
        self.assertEqual(args.ns.sp, 'p0')
        self.assertEqual(args.ns.arg0, '1')
        self.assertEqual(args.ns.arg1, '2')

        parser, args = p.parse_args('p1 a b c'.split())
        self.assertEqual(args.ns.sp, 'p1')
        self.assertEqual(args.ns.arg0, 'a')
        self.assertEqual(args.ns.arg1, 'b')
        self.assertEqual(args.ns.p1_arg0, 'c')
Example #7
0
    def test_to_dict(self):
        parser = Parser('name', 'description',
            Arg('a', 'arg a'),
            Arg('b', 'arg b'),
            Arg('c', 'arg c'))

        _, args = parser.parse_args('1 2 3'.split())
        dic = dict(args.ns)
        self.assertEqual(dic['a'], '1')
        self.assertEqual(dic['b'], '2')
        self.assertEqual(dic['c'], '3')
Example #8
0
def main():
    import os
    from terseparse import Parser, Arg, KW

    logging.getLogger().setLevel(logging.DEBUG)
    logging.basicConfig()

    p = Parser('dcmviewer', 'Display dicom series for viewing and editing ROIs',
            Arg('--version', 'show version', action='version',
                version='%(prog)s ({})'.format(__version__)),
            Arg('directory', 'Working directory to start the viewer in',
                default=os.path.abspath('.'), nargs='?'))
    _, args = p.parse_args()
    view(args.ns.directory)
Example #9
0
#!/usr/bin/env python
from terseparse import Arg, KW, Parser, SubParsers, types

description = '''shows an example of using subparsers'''


parser = Parser(__file__, description,
        Arg('--arg0', 'arg 0', type=types.Int()),
        SubParsers('command', '', KW(dest='command'),
            Arg('common', 'this argument is common to all sub parsers', type=int),
            Parser('a', 'parser a',
                Arg('arg0', 'arg 0', type=int),
                Arg('arg1', 'arg 1', type=int),),
            Parser('b', 'parser b',
                Arg('arg0', 'arg 0', type=str),
                Arg('arg1', 'arg 1', type=str))))

_, args = parser.parse_args()
args.pprint()
Example #10
0
#!/usr/bin/env python
from terseparse import Parser, Arg

description = '''Process some integers
This example is adapted to terseparse syntax from the argument parser library
'''

p = Parser('cmd', description,
    Arg('integers', 'an integer for the accumulator',
        metavar='N', type=int, nargs='+'),
    Arg('--sum', 'sum the integers (default: find the max)',
        dest='accumulate', action='store_const',
        const=sum, default=max))

_, args = p.parse_args()
args.pprint()
import pdb; pdb.set_trace()
print(args.ns.accumulate(args.ns.integers))