Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on For scripting what are the pros and cons of command line arguments versus capturing input at the start?
Parent
For scripting what are the pros and cons of command line arguments versus capturing input at the start?
Let's say I have a script that needs the user to set X number of variables at the start. One can either
- Pass the arguments in on the command line.
- Start the program and then have the user input the variables with Python's
input()
function or PHP'sfopen("php://stdin", "r")
for example.
What would the pros and cons be and when would I decide to use one method versus the other?
Pros of CLI arguments/flags include: Easier to leverage the tool in another script or via other automation so that …
4y ago
I'd like to move the frame out a little and use that as a discussion for how to make these decisions. Input models …
4y ago
I'm coming down strongly in favor of command-line arguments or options over interactive I/O for a number of reasons: …
1y ago
I would like to add further options to the set of possibilities, namely environment variables and GUI input. Moreover, …
1y ago
@laserkittens and @dmckee has already provided good answers, and I will not copy what's there. Personally, I treat …
4y ago
I largely agree with existing answers, but wanted to add this: In many cases, there is no clear answer one way or the …
1y ago
Post
Pros of CLI arguments/flags include:
-
Easier to leverage the tool in another script or via other automation so that user interaction is not required
-
If certain arguments are optional and/or have default values, the user is not burdened with these choices when they are unimportant
-
Once someone has used the tool a few times, it is often faster for them to memorize their preferred parameters or save/alias them than to have to manually input them each time
Cons may include:
-
If the targeted users are not comfortable using a CLI, they may struggle to use the application. Receiving parameters via interactive input may be more user-friendly (especially if interactive error handling/validation is employed to give the user immediate feedback and assistance with invalid options)
-
If the parameters are complex and certain options have conditional relationships upon others, gathering this information at runtime may be more user-friendly
Other considerations:
-
Consider accepting either CLI parameters or gathering parameters at runtime if no arguments are provided
-
Consider a configuration file if options will rarely change or are private, such as passwords/keys
-
If the script operates on a file, also consider receiving input via stdin along with various parameters, which enables data to be piped to the application from multiple sources (i.e., not just from a file, or from a file after being filtered through another tool)
1 comment thread