Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with click
#7
(Oct-10-2016, 02:57 PM)Actually, I didn\t ask what would be the output of the code. I asked what will be passed to param and value of callback function print_version.  snippsat Wrote:
# Do not call you file test.py
#foo.py
import click

def print_version(ctx, param, value):
    if not value or ctx.resilient_parsing:
        print('I am active now and return out') #Just to see when active
        return
    click.echo('Version 1.0')
    ctx.exit()

@click.command()
@click.option('--version', is_flag=True, callback=print_version, expose_value=False, is_eager=True)
def main():
   click.echo('Hello world')

if __name__ == '__main__':
   main()
Output:
E:\1_python\click λ python foo.py I am active now and return out Hello world E:\1_python\click λ python foo.py --version Version 1.0
If not use --version  flag,print_version function will do nothing(return).
If --version flag is used value from function return Version 1.0.
Quote:The expose_value parameter prevents the pretty pointless version parameter from being passed to the callback.
If that was not specified, a boolean would be passed to the hello script.
The resilient_parsing flag is applied to the context,
if Click wants to parse the command line without any destructive behavior
that would change the execution flow.
In this case, because we would exit the program, we instead do nothing.
Reply


Messages In This Thread
Help with click - by dullboy - Oct-09-2016, 07:54 AM
RE: Help with click - by Mekire - Oct-09-2016, 08:11 AM
RE: Help with click - by dullboy - Oct-09-2016, 08:57 AM
RE: Help with click - by snippsat - Oct-09-2016, 03:31 PM
RE: Help with click - by dullboy - Oct-10-2016, 12:32 AM
RE: Help with click - by snippsat - Oct-10-2016, 02:57 PM
RE: Help with click - by dullboy - Oct-10-2016, 04:16 PM
RE: Help with click - by snippsat - Oct-10-2016, 04:45 PM
RE: Help with click - by dullboy - Oct-10-2016, 04:53 PM
RE: Help with click - by Mekire - Oct-10-2016, 06:34 PM
RE: Help with click - by dullboy - Oct-11-2016, 12:30 PM
RE: Help with click - by metulburr - Oct-11-2016, 12:36 PM
RE: Help with click - by dullboy - Oct-11-2016, 12:47 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020