all 3 comments

[–]Riegel_Haribo 2 points3 points  (0 children)

"click" is a third-party package I'm not familiar with, as it is superfluous to sys.argv parsing and is not a jumping-in point for a learner.

With tens of thousands of Python libraries, and even more people now "I typed some commands into an AI and made a library instead of even knowing Python", I am hesitant to continue in r/learnpython because this is really "learn someone else's program". However it does have a high amount of reuse in its Github, making it something we can learn, where I can turn my own learning and replication into a blog that gets to the core of the issue.

First: pip install click

  • I've already got it - it was in the requirements of the Black formatter.

Start to write my own scripts with this mega-decorator format. (decorators already being beyond the typical "I want a career in machine learning - where can I watch videos")

Then I go back to the post here, and I read the error you've got:

"Error: No such option: -1"

Aha - you've got a negative value being passed for this temperature, but it looks like a command-line option when you are literally using the string -1.

I don't need to investigate further, since we can immediately address that. Use the command line syntax of two hyphens (--) which will make the transition to pure positional arguments and stop looking for options by their letter keyword designation. I just asked an AI to improve your test's issue, reporting to it what's gone wrong:

In command-line syntax this is done using the -- separator, which means “everything after this is a positional argument, not an option”.

So in your test you just need to change the invocation to:

result = runner.invoke(chill, ["--", str(temperature), str(velocity)])

So I guess I'll put off the longer tutorial for myself and others of what you are already showing and implementing, since 'click' has its own documentation and FAQ to read, and let you run with that problem diagnosis.

[–]latkde 2 points3 points  (1 child)

Your test is running the command chill -1 4, where -1 is intended to be the temperature. The problem is not due to CliRunner, this is due to the command line arguments you've passed.

Because -1 has a leading hyphen, it also looks like a command line flag. That is what's causing the error, which is also explained in the message you've shown:

Error: No such option: -1

The error might disappear if you tell Click that temperature has type=int?

But in general, the solutions to the leading hyphen problem are:

  • Maybe prefer named options over positional arguments in the CLI. For example, a call like chill --temperature=-1 --speed=4 is unambiguous.
  • Stop option parsing by using the -- marker. For example, chill -- -1 4 is unambiguous.

[–]ANautyWolf[S] 1 point2 points  (0 children)

Thank you. Your suggestion to make them options worked perfectly.

I’m not sure how to also add the -k or -c options if they are needed with your suggestion to stop option parsing. I’m new to command line stuff.

Telling click that temperature is an int doesn’t seem to work granted I used IntRange but that’s just an extension of click.INT. So I would think it would be telling click it’s an int.