-1

I'm trying to get the command line args before they get processed by click:

print(click.get_current_context().find_root().params)

This just prints empty eventhough I gave script cmd1 cmd2 --arg1 --arg2 3. I'm trying to get a string or list that would show script, cmd1 etc.

Update: Looks like I am able to access sys.argv I thought click "consumes" it but looks ok. However I don't know how reliable that is, what if some code modifies it? I looked at passing it as a meta but could not find the a good place to call get-current-context.

Oliver
  • 27,510
  • 9
  • 72
  • 103

1 Answers1

0

I can get

  • Script name: using get_current_context().info_name
  • Passed arguments as a dictionary using: get_current_context().params

I am using Click==7.0 in Python 3.6.8 environment.

Code:

import click

@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
              help='The person to greet.')
def hello(count, name):
    print(click.get_current_context().info_name)
    print(click.get_current_context().params)

if __name__ == '__main__':
    hello()

Program output:

python sample.py --count 2 --name "shovon"
sample.py
{'count': 2, 'name': 'shovon'}

I have used the sample program from Click official documentation.

arshovon
  • 13,270
  • 9
  • 51
  • 69
  • Yeah but for some reason when you are in a nested context this does not work (as you can see you will need find_root() and that's what I tried), can you give that a shot? – Oliver Nov 06 '19 at 23:34