[GRASS-dev] Possible to use "conditionals" for script options, flags?

Scripting in Python and defining options and flags in the header, as usual using the '#%' markers in the beginning of the line and following g.parser's manual. What if there is need to override an option if another one is given?

For example, an option considered as required may be the "mean target altitude" for i.atcorr's parameter file. What if an elevation map is given, which renders the previous option not-required?

What is the common practice for this? Define both options as optional and do the necessary checks inside the script? Or, is there a way to override options if another option is given in the definition-header?

Thanks, Nikos

Hi Nikos,

there is a new parser functionality which allows to specify the dependencies.There is no documentation, here are the C functions:
http://grass.osgeo.org/programming7/parser__dependencies_8c.html

recently Glynn added this for python scripts to, i am not sure about the syntax, perhaps something like this (just guess):
#%rules
#% exclusive: -a, -b
#% requires_all: opt1, opt2, -a
#%end

···

On Fri, Nov 21, 2014 at 11:43 AM, Nikos Alexandris <nik@nikosalexandris.net> wrote:

Scripting in Python and defining options and flags in the header, as usual using the ‘#%’ markers in the beginning of the line and following g.parser’s manual. What if there is need to override an option if another one is given?

For example, an option considered as required may be the “mean target altitude” for i.atcorr’s parameter file. What if an elevation map is given, which renders the previous option not-required?

you can specify that one of mean target altitude and elevation map is required (G_option_required) and that they are exclusive (G_option_exclusive), again, not sure about the Python syntax

What is the common practice for this? Define both options as optional and do the necessary checks inside the script? Or, is there a way to override options if another option is given in the definition-header?

Thanks, Nikos


grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

Anna Petrášová wrote:

Hi Nikos,

there is a new parser functionality which allows to specify the
dependencies.There is no documentation, here are the C functions:
http://grass.osgeo.org/programming7/parser__dependencies_8c.html

recently Glynn added this for python scripts to, i am not sure about the
syntax, perhaps something like this (just guess):
#%rules
#% exclusive: -a, -b
#% requires_all: opt1, opt2, -a
#%end

Awesome! Will look into it (while working hard at new python scripts).

By the way, your new creation is nice. I had a crash but didn't have time to reproduce the crash so I didn't report anyhting yet.

Cheers, Nikos

[rest deleted]

Nikos Alexandris wrote:

What is the common practice for this? Define both options as optional
and do the necessary checks inside the script? Or, is there a way to
override options if another option is given in the definition-header?

Marking an option as "required" will result in the parser raising a
fatal error if the option is not given, with one exception: if a flag
has the suppress_required option, and that flag is given, all
requirements are ignored. This feature is intended for flags which
abandon "normal operation" for the module; e.g. r.in.gdal's -f flag
(list supported formats) uses it.

But in general, you cannot mark an option as required if it is
optional except for the special case of a suppress_required flag.

Historically, the solution was to make the option not requrired, then
explicitly check the combinations in the code, e.g. (for C)

  if (!(altitude->answer || elevation->answer))
      G_fatal_error(_("either %s= or %s= must be given"),
          altitude->key, elevation->key);

However, GRASS 7 now has the ability to specify option relationships
to the parser. For C, the relevant functions are those in
lib/gis/parser_dependencies.c.

For scripts, relationships are specified using a "rules" section, e.g.

  #%rules
  #%required altitude,elevation
  #%end

specifies that at least one of those options must be given. Both
options and flags can be specified (a leading "-" denotes a flag).

The available rule types are:

    exclusive - at most one of the options may be given

    required - at least one of the options must be given

    requires - if the first option is given, at least one of the
    subsequent options must also be given

    requires_all - if the first option is given, all of the
    subsequent options must also be given

    excludes - if the first option is given, none of the
    subsequent options may be given

    collective - all or nothing; if any option is given, all must
    be given

Note that the "required" rule was overlooked from g.parser; I've just
committed a fix now in r62850.

--
Glynn Clements <glynn@gclements.plus.com>