szaghi / FLAP

Fortran command Line Arguments Parser for poor people
149 stars 34 forks source link

How to use switch - switch passed w/o value #100

Closed GHNewbiee closed 1 year ago

GHNewbiee commented 1 year ago

I need a switch (eg. --output, -o ) with the following requirements:

ie, program should be possible to run by:

$ program -i datafile               # no result file
$ program -i datafile -o            # result file name (automatically) set by program
$ program -i datafile -o resultfile # result file name passed by user

The switch I have tried to use is:

call cli%add(switch = '--output', switch_ab  = '-o', &
  help = 'Absolute or relative path of result file', &
  required = .false., act = 'store', def = '',       &
  error = error)

Any workaround for this "crossword" w/o adding an extra logical switch so that -o would be omitted (see right below)?

call cli%add(switch = '--write', switch_ab  = '-w', &
  help = 'Write to file(s)', required = .false.,    &
  act = 'store_true', def = '.false.', error = error)

call cli%add(switch = '--output', switch_ab  = '-o',  &
  help = 'Absolute or relative path of result file ', &
  required = .false., act = 'store', def = '',        &
  error = error)
$ program -i datafile                    # no result file
$ program -i datafile -w                 # result file name (automatically) set by program
$ program -i datafile [-w] -o resultfile # result file name passed by user

It would be convenient if there were an extra logical argument to cli%add() such as reqval which allows an argument w/o value to be passed.

Tia

PS: There are (technical or financial) programs which run very often and their output file names are easily standardized like arg1_..._data1_..._date_time.res where argX and dataX are set through CL or input data.

szaghi commented 1 year ago

@GHNewbiee

I think have got the point, but until I'll try to implement it I am not completely sure, probably I'll start with the other enhancements that are more clear for me (but I you can give the priority and, in case, I can start with this if you prefer).

Kind regards

GHNewbiee commented 1 year ago

I do not have any specific priority. The scope of my program can be implemented w/o any of the suggestions I have made. They are all for the improvement of FLAP. As the creator of the FLAP you have to decide which suggestions are within the scope and the spirit of it. In addition, there is not any rush at all; think and take your time.

Thank you for all the responses!

Kind regards, too.

GHNewbiee commented 1 year ago

IMHO, you should spend more time thinking about the implementation of this enhancement than that of exclude. The former has to maintain compatibility which is more "concern-able/difficult". For the latter, compatibility is not a concern at all.

szaghi commented 1 year ago

@GHNewbiee

I have just uploaded a patched version that should implement what you required.

I have added a test (quite long, I am sorry, but I have needed to test many scenarios): there you can see the usage of the new attribute val_required, when it is set to false you can omit the value even if you pass the CLA switch relying on the default value. This new attribute applies to all kinds of CLA with store action attributes (scalar and list). It is required to be a named CLA, obviously.

It this implementation is good enough I will update the documentation soon.

Cheers

GHNewbiee commented 1 year ago

@szaghi I will try it within the week. If not, definitely by the end of it. Tal for the quick implementation of this enhancement.

GHNewbiee commented 1 year ago

@szaghi

when it is set to false you can omit the value even if you pass the CLA switch relying on the default value.

In summary:

is_switch_passed is_value_required is_value_declared final_passed_value
T T T declared
T T F error
T F T declared
T F F default
F T default
F F default

Is it correct?

szaghi commented 1 year ago

@szaghi

when it is set to false you can omit the value even if you pass the CLA switch relying on the default value.

In summary:

is_switch_passed is_value_required is_value_declared final_passed_value T T T declared T T F error T F T declared T F F default F T default F F default Is it correct?

Yes