开发者

Command line parameters or configuration file?

开发者 https://www.devze.com 2022-12-20 21:41 出处:网络
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 start

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.开发者_开发知识库


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.


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.


my vote = both ala mysqld.exe


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号