Пример #1
0
 def test_complete_command_arg(self):
     abc = shellish.Command(name='abc')
     abc.add_argument('--foo')
     root = shellish.Command(name='root')
     root.add_subcommand(abc)
     sc = contrib.SystemCompletion()
     root.add_subcommand(sc)
     line = 'root abc --f'
     os.environ["COMP_CWORD"] = '2'
     os.environ["COMP_LINE"] = line
     sc(argv='--seed %s' % line)
     self.assertEqual(sys.stdout.getvalue(), '--foo\n')
Пример #2
0
 def test_complete_names(self):
     abc = shellish.Command(name='abc')
     xyz = shellish.Command(name='xyz')
     root = shellish.Command(name='root')
     root.add_subcommand(abc)
     root.add_subcommand(xyz)
     sc = contrib.SystemCompletion()
     root.add_subcommand(sc)
     line = 'root a'
     os.environ["COMP_CWORD"] = '1'
     os.environ["COMP_LINE"] = line
     sc(argv='--seed %s' % line)
     self.assertEqual(sys.stdout.getvalue(), 'abc\n')
Пример #3
0
    def test_file_argument_defaults_not_found(self):
        def run(args):
            self.assertRaises(FileNotFoundError, args.foo().__enter__)

        cmd = shellish.Command(name='test', run=run)
        cmd.add_file_argument('--foo')
        cmd(argv='--foo doesnotexist')
Пример #4
0
    def test_command_init(self):
        class Foo(shellish.Command):
            """ foo """
            pass

        Foo(name='foo')
        shellish.Command(name='test')
Пример #5
0
    def test_file_argument_stdout(self):
        def run(args):
            with args.foo as f:
                self.assertIs(f, sys.stdout)

        cmd = shellish.Command(name='test', run=run)
        cmd.add_file_argument('--foo', mode='w')
        cmd(argv='--foo -')
Пример #6
0
    def test_file_argument_create(self):
        def run(args):
            with args.foo as f:
                self.assertEqual(f.name, 'makethis')
                f.write('ascii is okay')

        cmd = shellish.Command(name='test', run=run)
        cmd.add_file_argument('--foo', mode='w')
        cmd(argv='--foo makethis')
Пример #7
0
 def test_many_bool_arguments_consumed(self):
     cmd = shellish.Command(name='cmd')
     cmd.add_argument('--foo', action='store_true')
     cmd.add_argument('--bar', action='store_true')
     self.assertEqual(self.complete(cmd, '--fo'), {'--foo'})
     self.assertEqual(self.complete(cmd, '--foo'), {'--foo'})
     self.assertEqual(self.complete(cmd, '--foo '), {'--bar'})
     self.assertEqual(self.complete(cmd, '--bar '), {'--foo'})
     self.assertEqual(self.complete(cmd, '--bar --foo '), set())
Пример #8
0
    def test_file_argument_read_found(self):
        def run(args):
            with args.foo as f:
                self.assertTrue(f.name.endswith('exists'))
                self.assertTrue(f.mode, 'r')

        open('exists', 'w').close()
        cmd = shellish.Command(name='test', run=run)
        cmd.add_file_argument('--foo', mode='r')
        cmd(argv='--foo exists')
        cmd(argv='--foo ./exists')
Пример #9
0
    def test_allrunners(self):
        refcnt = 0

        def runner(args, **ign):
            nonlocal refcnt
            refcnt += 1

        a = shellish.Command(name='a',
                             prerun=runner,
                             run=runner,
                             postrun=runner)
        a(argv='')
        self.assertEqual(refcnt, 3)
Пример #10
0
 def test_file_arguments(self):
     cmd = shellish.Command(name='cmd')
     cmd.add_file_argument('--foo')
     with tempfile.TemporaryDirectory() as tmp:
         files = {'./one', './two'}
         cwd = os.getcwd()
         os.chdir(tmp)
         try:
             for x in files:
                 open(x, 'w').close()
             self.assertEqual(self.complete(cmd, '--foo'), {'--foo'})
             self.assertEqual(self.complete(cmd, '--foo '), files)
             self.assertEqual(self.complete(cmd, '--foo o'), {'./one'})
             self.assertEqual(self.complete(cmd, '--foo NO'), set())
         finally:
             os.chdir(cwd)
Пример #11
0
    def test_file_argument_defaults_stdio(self):
        flushed = False

        class Stdin(object):
            def flush(self):
                nonlocal flushed
                flushed = True

        stdin_setinel = Stdin()

        def run(args):
            with args.foo as f:
                self.assertIs(f, stdin_setinel)
                self.assertFalse(flushed)
            self.assertTrue(flushed)

        cmd = shellish.Command(name='test', run=run)
        cmd.add_file_argument('--foo')
        stdin = sys.stdin
        sys.stdin = stdin_setinel
        try:
            cmd(argv='--foo -')
        finally:
            sys.stdin = stdin
Пример #12
0
def cat1():
    """ Auto command cannot interact. """
    pass


@shellish.autocommand
def sub1(optional: int = 1):
    print("ran subcommand1", optional)


cat1.add_subcommand(sub1)

###############
# Composition #
###############
cat2 = shellish.Command(name='cat2', title='composition cat')

sub2 = shellish.Command(name='sub2', title='composition cat sub')
sub2.add_argument('--optional', type=int, default=2)
sub2.run = lambda args: print("ran subcommand2", args.optional)
cat2.add_subcommand(sub2)


###############
# Inheritance #
###############
class Cat3(shellish.Command):
    """ Inheritance cat. """

    name = 'cat3'
Пример #13
0
 def test_subcommand_name_at_construct(self):
     a = shellish.Command(name='a')
     b = shellish.Command(name='b')
     a.add_subcommand(b)
Пример #14
0
 def test_onlyrun(self):
     ok = object()
     a = shellish.Command(name='a', run=lambda _: ok)
     self.assertIs(a(argv=''), ok)
Пример #15
0
 def test_naked(self):
     a = shellish.Command(name='a')
     self.assertRaises(SystemExit, a, argv='')
Пример #16
0
 def test_dash_opt(self):
     c = shellish.Command(name='test_dash')
     c.add_argument('--foo-bar')
     args = c.argparser.parse_known_args(['--foo-bar', 'value'])[0]
     self.assertIn('foo_bar', args)
     self.assertEqual(args.foo_bar, 'value')
Пример #17
0
"""
Demo how to add your auto tab completers.
"""

import random
import shellish


def thing_completer(prefix, args):
    letters = 'qwertyuiopasdfghjklzxcvbnm'
    word = lambda: ''.join(random.sample(letters, random.randint(1, 16)))
    return set(word() for x in range(random.randint(1, 1000)))


thing = shellish.Command(name='thing', title='Demo Tab Completion')
thing.add_argument('--choices', choices=['one', 'two', 'three'])
thing.add_argument('--function', complete=thing_completer)
root = shellish.Command(name='root')
root.add_subcommand(thing)
root.get_or_create_session().run_loop()
Пример #18
0
 def test_no_title_desc_by_compose(self):
     f = shellish.Command(name='foo')
     self.assertEqual(f.title, None)
     self.assertEqual(f.desc, None)