As a normal user, how would I know how many arguments and what arguments I need to pass in a command line arguments program?

50 Views Asked by At

As a user, how would I know how many arguments and what arguments I need to pass in a command line arguments program, since there is no input function applied to the script with some sort of indication stating, enter your name or age or etc...or how a user know that age comes first or name come first or address ?

just a general question for my understanding, as im going through a python learning process.

3

There are 3 best solutions below

0
jeleo On

It is good practice to write programs in such a way that an error is raised if you specify the wrong number of arguments.

And you can always open the program itself and take a look :)

0
javakx On

Generally speaking, if the program isn't in any way interactive and replies entirely on arguments from the command line, then the best way to know its argument criteria is to ask it. Typically the argument --help will produce a list of available options and how to use them, like so: ls --help, or python somescript.py --help.

The exact option required to get help may vary from program to program. On MS-DOS /? was common, and some older Unix programs used -?, but generally speaking --help has been a standard convention for quite some time, and you should try that first before looking for other options.

Btw, if you want to write Python programs that output their own help information, there's handy libraries such as argparse that handle most of the hard work for you. The argparse library (a standard part of Python) accepts a specification of the types of arguments your Python script will accept, and it will validate the user's command line arguments for you to check they meet your requirements, and even print out the --help information or error messages as necessary.

4
Suramuthu R On

A normal user (Note: I mean only the user, and not a programmer or developer) doesn't need to know how many arguments or what arguments are required. It is the responsibility of the developer to give the program user friendly. For instance, for giving the approximate age of a user, user is asked to give only the year of birth. Whereas, if the program needs to give the accurate age, the user will be asked to give date, month and year of birth.

In either cases, the user doesn't need to remember what arguments to give. Because, the user is going to answer only the questions they are asked.

Coming to the second part of the question, Being a programmer, if you don't know which argument to write first and which next, There are better ways to call a function.

you can very well call a function like function_name(age=25, name='Pratik Vaidhya'). Here the order of the arguments doesn't matter if you mention the arguments' name as well. But remember, you must stick to the spelling and capitalisation as per the syntax.