szaghi / FLAP

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

Ignore unknown CLA names? #82

Closed amartinhuertas closed 4 years ago

amartinhuertas commented 6 years ago

Dear @szagui,

In my application, I have a subset of CLAs that have to be parsed by FLAP, and another subset of CLAs that have to be parsed by a third party library I am calling from my application. Is there anyway to set up FLAP such that unknown CLA names are just ignored when parsing? In this case, the FLAP main data structure should be in the same status as this unkown CLA names were not passed. In case this is not possible, how can FLAP be modified such that this is possible on demand?

Thanks in advance for your help! Best regards, @amartinhuertas.

szaghi commented 6 years ago

Dear @amartinhuertas ,

thank you for your feeback. The answer is no, currently if FLAP parses an unknown CLA it raises and error. I never focused it could be helpful in scenario like your to raise only a warning (or even do nothing) when an unknown CLA is passed. I'll try to implement such a feature, but I am not sure the timing, maybe I can try to implement it during the next week.

I am thinking to add a new implicit switch (like the --help one) that disable the raising of error for unknown CLA. What do you think about such an implementation? You could have something like

type(command_line_interface) :: cli

call cli%init(ignore_unknown_clas=.true., ...)
...
amartinhuertas commented 6 years ago

Thanks for your prompt answer.

I'll try to implement such a feature, but I am not sure the timing, maybe I can try to implement it during the > next week.

Thanks so much! I am not in a hurry. I have found a way to work-around it. (let the third-party library to get CLAS from a file)

I am thinking to add a new implicit switch (like the --help one) that disable the raising of error for unknown > CLA. What do you think about such an implementation? You could have something like

type(command_line_interface) :: cli

call cli%init(ignore_unknown_clas=.true., ...) ...

That's precisely what I need.

ShatrovOA commented 4 years ago

Dear @szaghi ,

Have you been thinking about implementing this feature? I am using FLAP to parse my own clas, but i need to pass PETSc clas also...

Best regards!

szaghi commented 4 years ago

@ShatrovOA

I am very sorry, I was focused into other bugs of my libraries and I forget to implement this feature.

Let me some days more.

Cheers

ShatrovOA commented 4 years ago

@ShatrovOA

I am very sorry, I was focused into other bugs of my libraries and I forget to implement this feature.

Let me some days more.

Cheers

Hi @szaghi ,

Did you forget to implement this feature once again?

Best regards!

szaghi commented 4 years ago

@ShatrovOA

During this week I should find the time to try to implement this feature

szaghi commented 4 years ago

Dear @amartinhuertas @ShatrovOA

I have just pushed a new version (v1.2.1) that implement this feature request. I report below the commit log:

Add ignore passed unknown CLAs, fix issue#82

The CLI can now be initialized with a flag for ignoring the passed CLAs
that are unknown. An error is still echoed, but the CLI parsing goes on
until the end without exit the flow. By default a passed unknown CLA
raises a blocking error as in the previous verions.

This change addresses the need by:

add a new option to `init` method of CLI object, e.g.

call cli%init(..., ignore_unknown_clas=.true.)

A dedicated test has been added, i.e.
`src/test/flap_test_ignore_unknown_clas.f90`

Side effects:

Nothing.

Let me know if this implementation is ok.

Cheers

ShatrovOA commented 4 years ago

Dear @szaghi

First of all thanks for your work. I have a question. My program is using MPI and my common usage of FLAP is

then on rank 0 I call parse method and bcast error flag. I call parse on purpose since I dont want to see multiple copies of some --help messages or --version messages. I noticed that I am getting non-zero error after calling parse with ignore_unknown_clas=.true..

My question is: are you planning to change one day error codes or I can safely ignore code 1004?

amartinhuertas commented 4 years ago

Great @szaghi ! Thanks so much for your work. @victorsndvg and me will test it and let you know if there is any issue with the update!

szaghi commented 4 years ago

Dear @szaghi

First of all thanks for your work. I have a question. My program is using MPI and my common usage of FLAP is

  • use flap
  • init
  • add
  • add_group
  • ...

then on rank 0 I call parse method and bcast error flag. I call parse on purpose since I dont want to see multiple copies of some --help messages or --version messages. I noticed that I am getting non-zero error after calling parse with ignore_unknown_clas=.true..

My question is: are you planning to change one day error codes or I can safely ignore code 1004?

Hi, this was a designed behavior: 1004 is the first error code that can be ignored because the user selected the ignore unknown clas. It is a sort of warning code. It should remain as is until you or other users request a different behavior.

szaghi commented 4 years ago

Great @szaghi ! Thanks so much for your work. @victorsndvg and me will test it and let you know if there is any issue with the update!

Great, thank you guys.

Cheers

victorsndvg commented 4 years ago

Dear @szaghi ,

I've been testing a little this new feature. Thanks for your effort!

I've seen how it works, but I would like to confirm this as the expected behaviour.

Something like that:

  1. If unknown_flags is activated, you can pass flags that are not managed by FLAP (non-FLAP-flags).
  2. These non-FLAP-flags must be located at the end of the flags list
  3. Flags after any non-FLAP-flag will be ignored.

I'm right? If yes, I think point 3 must be explicitly explained anywhere in docs.

Do you agree?

Best

szaghi commented 4 years ago

@victorsndvg

Yes (and not). Currently the behavior seems to be as your description, but the effect 3 is not a desired feature. When an unknown CLA is passed and the user have chosen to ignore unknown CLAs, at the first unknown CLA passed the error-warning code (1004) is triggered and an error prompted, but the CLAs parsing should go further. If a valid CLA is passed after an unknown one it must be correctly parsed, if not it is a bug. I have to check it, thank you very much for pointing it out.

victorsndvg commented 4 years ago

@szaghi ,

thanks for your quick response. Let me know your conclusion or any further change after checking it.

szaghi commented 4 years ago

@victorsndvg

I confirm, the valid CLAs passed after an unknown CLA are missed, this is a bug, I do not like to force valid CLAs being before unknown ones.

victorsndvg commented 4 years ago

@szaghi ,

I agree with you. I would like only to ignore anything that is out of the FLAP registered flags schema, but to accept any other flag as soon as FLAP recognise them

szaghi commented 4 years ago

@victorsndvg

A new release is online, with a bug fix. I'll wait for your feedback.

Cheers

victorsndvg commented 4 years ago

@szaghi

I tested it. It seems that the unkown-flag-order issue is solved, but I've found a new one. If I call some programs using FLAP with --help flag it crashes with segfault.

One of the programs compiled in debug mode show this error:

`` At line 1387 of file /home/sandman/Repos/gitlab/fempar/ThirdParty/FLAP/src/lib/flap_command_line_interface_t.F90 Fortran runtime error: Index '1' of dimension 1 of array 'self%clasg' above upper bound of 0

szaghi commented 4 years ago

@victorsndvg Sorry for the delay, I has been sick in the last days (my little daughter shared with me a virus...).

In the new version there is a fix (it seems that the invocation of help usage message was always bugged, but only the last version raised this). Let me know if the fix works.

Cheers

ShatrovOA commented 4 years ago

Dear @szaghi

I modified your example and --help, --version switches don't work for me at all:

program flap_test_ignore_unknown_clas

use flap, only : command_line_interface, ERROR_UNKNOWN_CLAS_IGNORED
use penf

implicit none
type(command_line_interface) :: cli       !< Command Line Interface (CLI).
character(99)                :: a_string  !< String value.
integer(I4P)                 :: error     !< Error trapping flag.

call cli%init(description = 'ignore unknown CLAs usage FLAP example', ignore_unknown_clas=.true.)
call cli%add(switch='--string', switch_ab='-s', help='a string', required=.false., act='store', def = 'asdf', error=error)
call cli%add_group(group='restart', description='Enables Restart')
call cli%add( group='restart',                                                          &
              switch='--number',                                                        &
              switch_ab='-n',                                                           &
              help='Set number of restart file',                                        &
              help_color='red',                                                         &
              help_style='italics_on',                                                  &
              required=.true.,                                                          &
              def = '-1',                                                               &
              act='store')
call cli%add_group(group='converter', description='Switch to Converter mode')
call cli%add( group = 'converter',                                                      &
              switch='--start',                                                         &
              switch_ab='-s',                                                           &
              help='Set initial file number that should be converted',                  &
              help_color='cyan',                                                        &
              help_style='italics_on',                                                  &
              required=.true.,                                                          &
              def = '-1',                                                               &
              act='store')
call cli%get(switch='-s', val=a_string, error=error)
! if (error /= ERROR_UNKNOWN_CLAS_IGNORED) stop
print '(A)', cli%progname//' has been called with the following argument:'
print '(A)', 'String       = '//trim(adjustl(a_string))
print '(A,I5)', 'Error code   = ', error
endprogram flap_test_ignore_unknown_clas

Best regards!

szaghi commented 4 years ago

Dear @ShatrovOA thank you for your feedback, but I do not understand the error you faced: do you mean that with your modified example the help message is not printed?

ShatrovOA commented 4 years ago

Dear @ShatrovOA thank you for your feedback, but I do not understand the error you faced: do you mean that with your modified example the help message is not printed?

I noticed that in my case program ignores --help flag and continues execution.

szaghi commented 4 years ago

@ShatrovOA

I verified, something strange happens with groups... I am try to fix this new bug

szaghi commented 4 years ago

@ShatrovOA

I have just uploaded a new version with a fix, let me know if it works

ShatrovOA commented 4 years ago

Dear @szaghi ,

Everything works for me now.

Thank you!

szaghi commented 4 years ago

@ShatrovOA you are welcome and thank you for your feedback.

@victorsndvg I am closing this issue, but feel free to re-open it if needed.

Cheers.

victorsndvg commented 4 years ago

Sorry for the late response,

everything is working fine for us now.

Hope you are getting better @szaghi !

Thanks for your work.

Best

szaghi commented 4 years ago

@victorsndvg Thank you victor!

I am getting better, but slowly...

Cheers