Best Practice Question: Parameters

I would like to write a new command that will use a lot of variables and constants. In other languages I have written a config file that I then read. In PHP, I passed the command a first parameter, say “XXX” and in would then read the “XXX.conf” file that contained a number of variables and constants. Typically, I had 10-15 parameters and some 20 different configuration files. Is there a best practice for this in g’mic?

Usually, the most sensible approach would be something like this:

skip ${1=2},${2=5},..... # Set default values
var_a,var_b,.....=$* # ..... implies any number of additional variables

Other than that, you basically do the math via code to get other variables. I’m not going to mention multiple possible set of arguments that are not just repeating arguments because that’s pretty complicated. But, this should get you started at least. Many of my filters can take in 16+ arguments, but you can still use them with providing few arguments because of the use of default variables.

# $1: Filename of the configuration file.
foo : skip ${1=""}

  # Load parameters from configuration file.
  if ['"$1"']!=0 l[] {
    it "$1" s -,{'\n'} y x a y z. -1,100%
    f. "x?i:inrange(j(1),_'a',_'z')||inrange(j(1),_'A',_'Z')?_'_':_'#'" s y discard 0 i[1--1] ('\n') a y
    run {t} rm
    onfail error "Bad configuration file." rm
  } fi

  # Define a command 'get_arg variable_name,default_value' to retrieve local variables.
  m "get_arg : if narg($_$""1) u $_$""1 else u $""2 fi"

  # Get variable values.
  smoothness=${"get_arg smoothness,10"}
  axis=${"get_arg axis,x"}

  # Apply filter.
  blur $smoothness
  mirror $axis

And your configuration looks like:

# conf.txt : Configuration file.

# axis=y

And you call your command like this:

$ gmic image.jpg foo conf.txt

Thanks, @David_Tschumperle. That was quick.