Posts: 18
Threads: 6
Joined: Oct 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.
Posts: 591
Threads: 26
Joined: Sep 2016
Oct-09-2016, 08:11 AM
(This post was last modified: Oct-09-2016, 08:11 AM by Mekire.)
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):
Posts: 18
Threads: 6
Joined: Oct 2016
(Oct-09-2016, 08:11 AM)Mekire Wrote: 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):
Thanks for your reply. But how are param and value passed to the callback function print_version?
Posts: 7,313
Threads: 123
Joined: Sep 2016
Oct-09-2016, 03:31 PM
(This post was last modified: Oct-09-2016, 04:41 PM by 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).
Posts: 18
Threads: 6
Joined: Oct 2016
Oct-10-2016, 12:32 AM
(This post was last modified: Oct-10-2016, 12:38 AM by dullboy.)
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]
Posts: 7,313
Threads: 123
Joined: Sep 2016
Oct-10-2016, 02:57 PM
(This post was last modified: Oct-10-2016, 03:12 PM by snippsat.)
# 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.
Posts: 18
Threads: 6
Joined: Oct 2016
Oct-10-2016, 04:16 PM
(This post was last modified: Oct-10-2016, 04:18 PM by dullboy.)
(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.
Posts: 7,313
Threads: 123
Joined: Sep 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.
Posts: 18
Threads: 6
Joined: Oct 2016
Oct-10-2016, 04:53 PM
(This post was last modified: Oct-10-2016, 06:40 PM by snippsat.)
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.
Posts: 591
Threads: 26
Joined: Sep 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...
|