Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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?

+12
−0

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's fopen("php://stdin", "r") for example.

What would the pros and cons be and when would I decide to use one method versus the other?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (1 comment)
Post
+15
−0

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)

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (5 comments)
General comments
Someone‭ wrote about 4 years ago

One point that seems to be missing: Command line parameters have a platformdependent limit on their length. You can nearly read unlimited input. This becomes very important when you need to handle many files and cannot rely on xarg or need to work in one run.

dustytrash‭ wrote about 4 years ago

The user doesn't need to worry about escaping quotes, and using quotes for arguments containing spaces with capturing input

klutt‭ wrote about 4 years ago

"If certain arguments are optional" - This is fairly easy to solve in both approaches. I don't think it counts as a difference between these two concepts.

qohelet‭ wrote about 4 years ago

@klutt true, depending on style of collecting params via stdin, usually clicking "Enter" is sufficient to accept default values. However, they could be ignored altogether in CLI params and just sensible defaults used (but I see your point that this could also be the case at runtime also if an alternative method of gathering params via stdin were used). The question is rather vague (no specific program info/objectives), so my answer is necessarily also so.

jrh‭ wrote about 4 years ago · edited about 4 years ago

"If the targeted users are not comfortable using a CLI" -- another thing to keep in mind that affects both methods, there's a good bit of ambiguity in the exact format needed for the arguments when they are typed in manually instead of, e.g., provided through a UI. E.g., does the program accept "parameter" or parameter? parameter=value? --parameter value? --parameter=value? --parameter="value"? I've seen a ton of variations on that, you have to document this sort of thing carefully.