示例#1
0
    def build_named_args_command(self):
        com = Command('named my_arg <-f filename> [-g othername]', 'Demonstrate arguments')
        def _val(*args, **kwargs):
            success,message = com.default_validate(*args, **kwargs)
            if not success:
                if len(args) == 0 and len(kwargs) == 0:
                    self.put("""
Some commands accept command line arguments.
When creating a command, you specify these arguments with a special (simple) syntax.
For example, this command is called "%s". It accepts both positional and named arguments.

Positional arguments are indicated by a bare word following the command's name
in the command definition.
Named arguments are wrapped in either <> or [], and contain both the argument name
(starting with '-') and a helpful tip about the argument function.
Named arguments with <> are required, those with [] are optional.

This command also demonstrates customizable tab completion hooks. Try pressing
Tab after typing '%s s' and before typing enter. You'll see a list of possible
completions for your argument.

You can customize these commands by supplying callbacks to Command.tabcomplete_hooks.


Try passing arguments to this command!
                    """ % (com.name, com.name))
                else:
                    self.put("""
This command requires one unnamed argument followed by a named argument (-f).
Try this:
%s helloworld -f data.txt
                    """ % com.name)
                return (False, message)
            return (success, message)
        com.validate = _val

        def _run(*args, **kwargs):
            self.put("Got arguments:")
            self.put(args)
            self.put(kwargs)
            self.put("Arguments are passed around in a format quite familiar to python:")
            self.put("Positional arguments in a list, named arguments in a dictionary")
            return constants.CHOICE_VALID
        com.run = _run

        def _complete_myarg(frag):
            return ['some', 'random', 'choices']
        com.tabcomplete_hooks['my_arg'] = _complete_myarg

        def _complete_file(frag):
            from os import listdir
            from os.path import isfile, join
            onlyfiles = [f for f in listdir('.') if isfile(join('.',f))]
            return onlyfiles
        com.tabcomplete_hooks['filename'] = _complete_file

        return com
示例#2
0
    def build_named_args_command(self):
        com = Command('named my_arg <-f filename> [-g othername]', 'Demonstrate arguments')
        def _val(*args, **kwargs):
            success,message = com.default_validate(*args, **kwargs)
            if not success:
                if len(args) == 0 and len(kwargs) == 0:
                    self.put("""
Some commands accept command line arguments.
When creating a command, you specify these arguments with a special (simple) syntax.
For example, this command is called "%s". It accepts both positional and named arguments.

Positional arguments are indicated by a bare word following the command's name
in the command definition.
Named arguments are wrapped in either <> or [], and contain both the argument name
(starting with '-') and a helpful tip about the argument function.
Named arguments with <> are required, those with [] are optional.

This command also demonstrates customizable tab completion hooks. Try pressing
Tab after typing '%s s' and before typing enter. You'll see a list of possible
completions for your argument.

You can customize these commands by supplying callbacks to Command.tabcomplete_hooks.


Try passing arguments to this command!
                    """ % (com.name, com.name))
                else:
                    self.put("""
This command requires one unnamed argument followed by a named argument (-f).
Try this:
%s helloworld -f data.txt
                    """ % com.name)
                return (False, message)
            return (success, message)
        com.validate = _val

        def _run(*args, **kwargs):
            self.put("Got arguments:")
            self.put(args)
            self.put(kwargs)
            self.put("Arguments are passed around in a format quite familiar to python:")
            self.put("Positional arguments in a list, named arguments in a dictionary")
            return constants.CHOICE_VALID
        com.run = _run

        def _complete_myarg(frag):
            return ['some', 'random', 'choices']
        com.tabcomplete_hooks['my_arg'] = _complete_myarg

        def _complete_file(frag):
            from os import listdir
            from os.path import isfile, join
            onlyfiles = [f for f in listdir('.') if isfile(join('.',f))]
            return onlyfiles
        com.tabcomplete_hooks['filename'] = _complete_file

        return com
示例#3
0
    def build_invalid_command(self):
        com = Command('broken', 'Demonstrate invalid command')
        def _run(*args, **kwargs):
            self.put("I will never print")
            return constants.CHOICE_VALID
        com.run = _run

        def _val(*args, **kwargs):
            message = """
You can write custom validation functions for your commands.
A validation function will run before execution of the command.
If the validation returns False, the command is not run.
The default validation function checks for the presence of all required
arguments, but you can override this behavior by setting the Command's validate member.

This is a command that always fails to validate.
            """
            return (False, message)
        com.validate = _val

        return com
示例#4
0
    def build_invalid_command(self):
        com = Command('broken', 'Demonstrate invalid command')
        def _run(*args, **kwargs):
            self.put("I will never print")
            return constants.CHOICE_VALID
        com.run = _run

        def _val(*args, **kwargs):
            message = """
You can write custom validation functions for your commands.
A validation function will run before execution of the command.
If the validation returns False, the command is not run.
The default validation function checks for the presence of all required
arguments, but you can override this behavior by setting the Command's validate member.

This is a command that always fails to validate.
            """
            return (False, message)
        com.validate = _val

        return com
示例#5
0
    def build_named_args_command(self):
        com = Command('named my_arg <-f filename> [-g othername]', 'Demonstrate arguments')
        def _val(*args, **kwargs):
            success,message = com.default_validate(*args, **kwargs)
            if not success:
                if len(args) == 0 and len(kwargs) == 0:
                    self.put("""
Some commands accept command line arguments.
When creating a command, you specify these arguments with a special (simple) syntax.
For example, this command is called "%s". It accepts both positional and named arguments.

Positional arguments are indicated by a bare word following the command's name
in the command definition.
Named arguments are wrapped in either <> or [], and contain both the argument name
(starting with '-') and a helpful tip about the argument function.
Named arguments with <> are required, those with [] are optional.


Try passing arguments to this command!
                    """ % com.name)
                else:
                    self.put("""
This command requires one unnamed argument followed by a named argument (-f).
Try this:
%s helloworld -f data.txt
                    """ % com.name)
                return (False, message)
            return (success, message)
        com.validate = _val

        def _run(*args, **kwargs):
            self.put("Got arguments:")
            self.put(args)
            self.put(kwargs)
            self.put("Arguments are passed around in a format quite familiar to python:")
            self.put("Positional arguments in a list, named arguments in a dictionary")
            return constants.CHOICE_VALID
        com.run = _run
        return com