Command line parameters or configuration file?
Asked Answered
C

6

30

I'm developing a tool that will perform several types of analysis, and each analysis can have different levels of thoroughness. This app will have a fair amount of options to be given before it starts. I started implementing this using a configuration file, since the number of types of analysis specified were little. As the number of options implemented grew, I created more configuration files. Then, I started mixing some command line parameters since some of the options could only be flags. Now, I've mixed a bunch of command line parameters with configuration files and feel I need refactoring.

My question is, When and why would you use command line parameters instead of configuration files and vice versa?

Is it perhaps related to the language you use, personal preference, etc.?

EDIT: I'm developing a java app that will work in Windows and Mac. I don't have a GUI for now.

Calefactory answered 23/2, 2010 at 23:41 Comment(0)
H
12

Command line parameters are useful for quickly overriding some parameter setting from the configuration file. As well, command line parameters are useful if there are not so many parameters. For your case, I'd suggest that you export parameter presets to command line.

Horseleech answered 23/2, 2010 at 23:45 Comment(0)
S
7

Command line arguments:

Pros:

  1. concise - no extra config files to maintain by itself
  2. great interaction with bash scripts - e.g. variable substitution, variable reference, bash math, etc.

Cons:

  1. it could get very long as the options become more complex
  2. formatting is inflexible - besides some command line utilities that help you parse the high level switches and such, anything more complex (e.g. nested structured information) requires custom syntax such as using Regex, and the structure could be quite rigid - while JSON or YAML would be hard to specify at the command line level

Configuration files:

Pros:

  1. it can be very large, as large as you need it to be
  2. formatting is more flexible - you can use JSON, YAML, INI, or any other structural format to represent the information in a more human consumable way

Cons:

  1. inflexible to interact with bash variable substitutions and references (as well as bash math) - you have to probably define your own substitution rules if you want the config file to be "generic" and reusable, while this is the biggest advantage of using command line arguments - variable math would be difficult in config files (if not impossible) - you have to define your own "operator" in the config files, or you have to rely on another bash script to carry out the variable math, and perform your custom variable substitution so the "generic" config file could become "concretely usable".
  2. for all that it takes to have a generic config file (with custom defined variable substitution rules) ready, a bash script is still needed to carry out the actual substitution, and you still have to code your command line to accept all the variable substitutions, so either you have config files with no variable substitution, which means you "hard code" and repeat the config file for different scenarios, or the substitution logic with custom variable substitution rules make your in-app config file logic much more complex.

In my use case, I value being able to do variable substitution / reference (as well as bash math) in the bash scripts more important, since I'm using the same binary to start many server nodes with different responsibilities in a server backend cluster, and I kind of use the bash scripts as sort of a container or actually a config file to start the many different nodes with differing command line arguments.

Stealer answered 4/10, 2017 at 22:17 Comment(0)
G
4

my vote = both ala mysqld.exe

Glidewell answered 23/2, 2010 at 23:44 Comment(0)
H
2

What environment/platform? In Windows you'd rather use a config file, or even a configuration panel/window in the gui.

Houseboat answered 23/2, 2010 at 23:52 Comment(1)
I'm developing a java app that will work in Windows and Mac. I don't have a GUI for now.Calefactory
E
0

I place configuration that don't really change in a configuration file. Configuration that change often I place on the command-line.

Epicotyl answered 10/6, 2022 at 12:55 Comment(0)
O
0

Use a rule of thumb, if you have more things that change a lot over the program's execution (say a variable called is_output that generates a report for every single analysis and then needs to be updated every time), that's a good candidate for an argument of even user input, use arguments.

Just a reminder when using user inputs, make sure to validade them just so they don't serve as an entry point for some kind of attack. You just never know!

The rest is best stored in a config file, whatever suits you the most, and if you want it to be reusable, come up with a syntax for variable replacement, for example {{ var }} or even $$ var $$ or the one I personally use @@ var @@.

Oligosaccharide answered 4/6 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.