Giter VIP home page Giter VIP logo

scripted's Introduction

Scripted

There are many libraries for argument parsing and command line tools; this is mine.

Example

  1. Define a controller class

    from scripted import Script
    
    
    script = Script()
    
    
    @script.add_controller
    @script.option('-a', '--option-a', dest='option_a', help='help text')
    @script.option('-b', '--option-b', dest='option_b', help='help text')
    class CommandController(script.Controller):
        """Main parser's help menu created from Controller class docstring.
    
        This is an example controller class used to make complex, deeply
        nested command line tools simple.
        """
    
        @script.argument('-c', '--c3', dest='a3', help='help text')
        @script.argument('c2', dest='c2', help='help text')
        @script.argument('c1', dest='c1', help='help text')
        def command_one(self):
            """Subparser help and desc created from command_one docstring."""
            self.fn.print(f"Called {self.fn.scope} with {self.args}")
            public = self.fn.prompt('username:', mode='stdin')
            private = self.fn.prompt('password:', mode='password')
            self.fn.print(public, private)
    
    
        @script.argument('-d', '--d3', dest='a3', help='help text')
        @script.argument('d2', dest='d2', help='help text')
        @script.argument('d1', dest='d1', help='help text')
        def command_two(self):
            """Sub-parser help menu created from command_two docstring."""
            self.fn.print(f"Called {self.fn.scope} with {self.args}")
    
    
        @script.argument('-e', '--e3', dest='a3', help='help text')
        @script.argument('e2', dest='e2', help='help text')
        @script.argument('e1', dest='e1', help='help text')
        def command_three(self):
            """Subparser help menu created from command_three docstring."""
            self.fn.print(f"Called {self.fn.scope} with {self.args}")
    
    
        def helper(self):
            """Undecorated methods are not introspected."""
            self.fn.print('Helper method')
    
    
    if __name__ == '__main__':
        script.execute()
  2. Call the script however python scripts are called.

    python myapp.py -h
    python myapp.py command_one -h    

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.