Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Click Click
#7
(Apr-16-2017, 07:28 AM)wavic Wrote: However, I want to know, how to get positional arguments with Click?
Something like this.
If nargs is set -1,then an unlimited number of arguments is accepted.
#numb_input.py
import click

@click.command()
@click.argument('n', nargs=-1)
def numbers(n):
    click.echo(n)
    click.echo(sum(int(i) for i in n))

if __name__ == '__main__':
    numbers()
Output:
(click) C:\Python36\click λ python numb_input.py 1 2 3 4 5 ('1', '2', '3', '4', '5') 15 (click) C:\Python36\click λ python numb_input.py 100 458 96878 ('100', '458', '96878') 97436
As a example sum first numbers,then make a dict of last argument.
#numb_input_1.py
import click

@click.command()
@click.argument('n', nargs=-1)
@click.argument('d', nargs=1)
def numbers(n, d):
    click.echo(sum(float(i) for i in n))
    to_dict = dict([d.split('=')])
    click.echo(to_dict)

if __name__ == '__main__':
    numbers()
Output:
(click) C:\Python36\click λ python numb_input_1.py 100 458 96878 my_number=666 97436.0 {'my_number': '666'}
Reply


Messages In This Thread
Click Click - by snippsat - Mar-07-2017, 12:04 AM
RE: Click Click - by wavic - Mar-07-2017, 06:17 AM
RE: Click Click - by snippsat - Mar-07-2017, 12:34 PM
RE: Click Click - by snippsat - Mar-10-2017, 07:52 PM
RE: Click Click - by GamingAgent - Apr-15-2017, 09:00 AM
RE: Click Click - by wavic - Apr-16-2017, 07:28 AM
RE: Click Click - by snippsat - Apr-16-2017, 12:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  wxpython single click/double click recognition Larz60+ 1 6,187 Jul-23-2017, 06:49 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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