Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with click
#1
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.
Reply
#2
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):
Reply
#3
(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?
Reply
#4
(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).
Reply
#5
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]
Reply
#6
# 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
#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
#8
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.
Reply
#9
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.
Reply
#10
(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...
Reply


Forum Jump:

User Panel Messages

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