Help with click - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help with click (/thread-395.html) Pages:
1
2
|
Help with click - dullboy - Oct-09-2016 Here is the code, #test.py import click def print_version(ctx, value): if not value or ctx.resilient_parsing: 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()When I run the code with test.py --version 1.0,I got a Warning: Invoked legacy parameter callback "<function print_version at 0x0000000002BE2A58>". The new signature for such callbacks starting with click 2.0 is (ctx, param, value). value, args = param.handle_parse_result(ctx, opts, args) What does it mean? Thanks. RE: Help with click - Mekire - Oct-09-2016 Not familiar with click, but as it says, try adding param to the callback signature. Line 3 should look like this: def print_version(ctx, param, value): RE: Help with click - dullboy - Oct-09-2016 (Oct-09-2016, 08:11 AM)Mekire Wrote: Not familiar with click, but as it says, try adding param to the callback signature. Thanks for your reply. But how are param and value passed to the callback function print_version? RE: Help with click - snippsat - Oct-09-2016 (Oct-09-2016, 08:57 AM)dullboy Wrote: Thanks for your reply. But how are param and value passed to the callback function print_version?It's explained here. Quote:Not familiar with click, but as it says, try adding param to the callback signature.It's pretty impressive,it blows other command line parser out the window :whistle: Just the fact that click.echo(''), has full Unicode support and you will get correct output in all OS(also in Windows cmd where Unicode always has been wrong). RE: Help with click - dullboy - Oct-10-2016 Thanks for your reply. But I still don't quite understand the tutorial you referenced. Can you walk me through the example in the following? #test.py import click def print_version(ctx, param, value): if not value or ctx.resilient_parsing: 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()In my example, what will be passed to param and value separately in the callback function print_version? snippsat (Oct-09-2016, 08:57 AM)dullboy Wrote: Thanks for your reply. But how are param and value passed to the callback function print_version?It's explained here. Quote:Not familiar with click, but as it says, try adding param to the callback signature.It's pretty impressive,it blows other command line parser out the window :whistle: Just the fact that click.echo(''), has full Unicode support and you will get correct output in all OS(also in Windows cmd where Unicode always has been wrong).[/quote] RE: Help with click - snippsat - Oct-10-2016 # 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() 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. RE: Help with click - dullboy - Oct-10-2016 (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()If not use --version flag,print_version function will do nothing(return). RE: Help with click - snippsat - Oct-10-2016 Quote:Actually, I did'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. All is explained here Callbacks and Eager Options You can test yourself with print() as many forget. def print_version(ctx, param, value): print(value, ctx.resilient_parsing)Use should see the boolan values True,False as it passed to function. RE: Help with click - dullboy - Oct-10-2016 I am not sure if you really know the answer. snippsat Quote:Actually, I did'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. All is explained here Callbacks and Eager Options You can test yourself with print() as many forget. def print_version(ctx, param, value): print(value, ctx.resilient_parsing)Use should see the boolan values True,False as it passed to function. RE: Help with click - Mekire - Oct-10-2016 (Oct-10-2016, 04:53 PM)dullboy Wrote: I am not sure if you really know the answer. You are getting hung up on something that doesn't matter at all to you using this library. The callback is passed the ctx object, an Options object, and a boolean value. The boolean value is True or False depending on whether the flag for that option is present. Add some print lines in the callback and print out the passed arguments... |