Skip to content
Snippets Groups Projects
Select Git revision
  • v17.7.7
  • gitlab/15-0-stable-salsa default
  • 18-2-stable-salsa protected
  • 18-1-stable-salsa protected
  • 18-0-stable-salsa protected
  • 17-11-stable-salsa protected
  • 17-10-stable-salsa protected
  • 17-9-stable-salsa protected
  • 17-8-stable-salsa protected
  • 17-7-stable-salsa protected
  • 17-6-stable-salsa protected
  • 17-5-stable-salsa protected
  • 17-4-stable-salsa protected
  • 17-3-stable-salsa protected
  • 17-2-stable-salsa protected
  • 17-1-stable-salsa protected
  • 17-0-stable-salsa protected
  • 16-11-stable-salsa protected
  • 16-10-stable-salsa protected
  • 16-9-stable-salsa protected
  • 16-8-stable-salsa protected
  • 16-7-stable-salsa protected
  • v18.2.8-salsa-1 protected
  • v18.1.6-salsa-2 protected
  • v18.3.5
  • v18.4.3
  • v18.5.1
  • v18.5.0
  • v18.5.0-rc43
  • v18.5.0-rc42
  • v18.2.8
  • v18.3.4
  • v18.4.2
  • v18.2.7
  • v18.3.3
  • v18.4.1
  • v18.4.0
  • v18.4.0-rc43
  • v18.4.0-rc42
  • v18.1.6
  • v18.1.6-salsa-1 protected
  • v18.2.6
42 results

index.md

Blame
  • first_app.py 1.78 KiB
    #!/usr/bin/env python
    # coding=utf-8
    """
    A simple application using cmd2 which demonstrates 8 key features:
    
        * Settings
        * Commands
        * Argument Parsing
        * Generating Output
        * Help
        * Shortcuts
        * Multiline Commands
        * History
    """
    
    import cmd2
    
    
    class FirstApp(cmd2.Cmd):
        """A simple cmd2 application."""
    
        def __init__(self):
            shortcuts = cmd2.DEFAULT_SHORTCUTS
            shortcuts.update({'&': 'speak'})
            super().__init__(multiline_commands=['orate'], shortcuts=shortcuts)
    
            # Make maxrepeats settable at runtime
            self.maxrepeats = 3
            self.add_settable(cmd2.Settable('maxrepeats', int, 'max repetitions for speak command', self))
    
        speak_parser = cmd2.Cmd2ArgumentParser()
        speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
        speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
        speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
        speak_parser.add_argument('words', nargs='+', help='words to say')
    
        @cmd2.with_argparser(speak_parser)
        def do_speak(self, args):
            """Repeats what you tell me to."""
            words = []
            for word in args.words:
                if args.piglatin:
                    word = '%s%say' % (word[1:], word[0])
                if args.shout:
                    word = word.upper()
                words.append(word)
            repetitions = args.repeat or 1
            for _ in range(min(repetitions, self.maxrepeats)):
                # .poutput handles newlines, and accommodates output redirection too
                self.poutput(' '.join(words))
    
        # orate is a synonym for speak which takes multiline input
        do_orate = do_speak
    
    
    if __name__ == '__main__':
        import sys
    
        c = FirstApp()
        sys.exit(c.cmdloop())