1

I'm using a python library that has a configuration step which involves calling a function and providing the API key in the prompt response. It uses input(). This function creates the file necessary with all the configurations.

This is fine for humans to use the API, but I'm adding CI to my code and wish to unit tests calls to this API. As such, I need to complete the same step.

I simply want to do something like python -c '<PYTHON CODE>'.

Aside from altering the code to allow for an optional input of the API key (to skip the prompt), I wonder if there is an easier way to do this with sys.argv.

I did some googling and could not find a working example involving input() and argument inputs.

Thoughts appreciated.

8
  • Take a look at the argparse library. Commented Oct 29, 2021 at 18:40
  • Familiar with argparse, but I don't see how I could pass this into a call of api_key = input("what's your API key? : ") Commented Oct 29, 2021 at 18:51
  • Either you input using input(), or using an argument parser, such as sys.argv or, better, argparse, as mentioned by @enzo. Or even both, and pick one to use at runtime. It is unclear to me what exactly you want or expect as an answer Commented Oct 29, 2021 at 19:15
  • @MatBBastos, I'm trying to keep my question general while still explaining context. In short, is there a way to pass arguments to functions that use input() without modifying the original code? I just don't want it to prompt for human input. Commented Oct 29, 2021 at 19:20
  • Would you be executing the original code from a file, without 'seeing' it? If you are executing it as a script, you can make an 'outer script' that executes it and provides input when necessary. That doesn't even have to be in Python, though it could. Commented Oct 29, 2021 at 19:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.