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
I largely agree with existing answers, but wanted to add this:
In many cases, there is no clear answer one way or the other. Take the program wc
which counts characters and lines. Should the interface be wc file.txt
or cat file.txt | wc
? Compelling arguments can be made for the value of either. Yet we have to pick one.
Actually we don't. wc
supports both. More complex programs support the well known -
parameter, so cat file.txt | some_program --file -
as a way of making it a bit more coherent.
Interactive input falls under this as well. ssh-keygen
will interactively ask for the key type. But with ssh-keygen -t ed25519
it will not ask.
CLI arguments, standard input (from pipe) and interactive input form the three pillars of Unix-style human-computer interaction. All programs should aspire to provide first-class support for all three eventually, even if they won't, just like "all kids" aspire to be astronauts. Imagine a stool with two legs. You can make it work, and it's certainly better than a stool with one leg, and way better than none, but once you get to three legs it gets really good.
Of course you have to start somewhere. For your first leg, just pick one. Whatever you feel is a likely use case for your script, and let users rely on Unix to "improvise" the missing legs:
-
cat
if they want to use args but you support stdin - Shell redirection if they want stdin but you want args
-
expect
if you support interactive but they want scriptable
As your program matures, you can use the experience gained from maintaining it to decide which next method of input you should add support for.
1 comment thread