Appendix of a short version of the click library reference



  • I'll use the click library to process the violin start parameters. This library can create a parameter on its own. --help for the team.

    I had a desire to add the possibility of using a short version of the call team. -h

    How can this be done?



  • In all the examples of the code, it is understood that the click module is imported and the required team is called, the code is stored in the test.py file.

    There were two ways to add that functionality.


    Use of decorator http://click.pocoo.org/6/api/#click.help_option

    @click.command()
    @click.help_option('--help', '-h')
    def test():
        print('Hello!')
    

    In this case, both a long and short version can be used, but I can't leave the short version alone.:

    @click.command()
    @click.help_option('-h')
    def test():
        print('Hello!')
    

    The reference will be as follows:

    Usage: test.py [OPTIONS]
    

    Options:
    -h Show this message and exit.
    --help Show this message and exit.

    That's what every team makes a parameter to help default. If this opportunity is disconnected, the automatic long version of the team will fall, and the reference will look as expected.

    @click.command(add_help_option=False)
    @click.help_option('-h')
    def test():
    print('Hello!')

    Use of parameter context_settings At the decorator. http://click.pocoo.org/5/api/#click.command

    @click.command(context_settings={'help_option_names': ['-h', '--help']})
    def test():
    print('Hello!')

    Parameter context_settings to the decorator @click.command eventually used http://click.pocoo.org/5/api/#context team.

    List help_option_names default consists of one line '--help'if this list is to be replaced by its own, a team can be set up to challenge the information in an arbitrary manner.


    Sources:

    • http://click.pocoo.org/6/api/#click.help_option
    • http://click.pocoo.org/5/api/#click.command


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2