1

I am trying to add a feature to my program where the user can interact with the program to run specific functions. But the best I can think of is this:

while input != "Exit" and input != "Quit":
    if input() == 'command' + arguments:
        Do X
    elseif input() == 'Othercommand' + arguments:
        Do Y

This doesn't sound like a good solution though.

This program will only be used by me so it doesn't need to be secure in terms of input checks. It is not really big enough project to use a GUI either (though I have done that before).

The program is going to be a webscraper where I can output the data as a JSON file, and read lists to go and scrape different websites, so this will have different functions like read file, save file, update data, etc. I am not sure whether this is the best idea for this type of program so any suggestions about different ideas would be welcome.

mkrieger1
  • 14,486
  • 4
  • 43
  • 54
  • use argparse. https://docs.python.org/3/library/argparse.html – Adithya Shetty Dec 17 '20 at 03:01
  • this may help https://docs.python-guide.org/scenarios/cli/ – sahasrara62 Dec 17 '20 at 03:09
  • Does this answer your question? [how to make a Command Line Interface or Interpreter in python](https://stackoverflow.com/questions/3910160/how-to-make-a-command-line-interface-or-interpreter-in-python) – mkrieger1 Dec 24 '20 at 23:27
  • Does this answer your question? [Implementing a "\[command\] \[action\] \[parameter\]" style command-line interfaces?](https://stackoverflow.com/questions/362426/implementing-a-command-action-parameter-style-command-line-interfaces) – Gino Mempin Dec 24 '20 at 23:33

2 Answers2

1

If you want to have a proper command-line interface for your application, the argparse is the module to go.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--name', dest='name', type=str, help='Name of the candidate')
parser.add_argument('--surname', dest='surname', type=str, help='Surname of the candidate')
parser.add_argument('--age', dest='age', type=int, help='Age of the candidate')

args = parser.parse_args()
print(args.name)
print(args.surname)
print(args.age)

Kickstart tutorial

RCvaram
  • 3,901
  • 3
  • 16
  • 32
  • 1
    I think this is what I was looking for. It looks like it will do what I needed. I'll take a look. Thanks. Does this handle the program waiting for inputs also, or will that need to be done with a while loop in my example? – monkeyboy401 Dec 17 '20 at 03:22
  • You can send the arguments while starting the application – RCvaram Dec 17 '20 at 03:34
  • python myapp.py --name monkeyboy --age 18 – RCvaram Dec 17 '20 at 03:35
  • Look at the kickstart tutorial, For more info – RCvaram Dec 17 '20 at 03:36
  • Is it possible to have it keep accepting arguments after starting the application? A bit like a GUI application. – monkeyboy401 Dec 17 '20 at 12:52
  • I think it is not needed since if you want to quit an application with user keyboard input you can use sys module exit command – RCvaram Dec 17 '20 at 12:56
  • I would like the application to ask for user input where it will do different functions based on the input. After it has finished each function it will wait for the next input. The exit is just for the user to quit the program. Will argparse be able to do something like this? – monkeyboy401 Dec 17 '20 at 14:37
  • I just found python CMD module which is much closer to what I was looking for. Thanks for the other replies though. – monkeyboy401 Dec 24 '20 at 23:01
-2

The CMD Python Module allows for a User to input multiple commands to a python program.