Open urbanjost opened 4 years ago
PS; There is a more extensive M_args.f90 included in the "General Purpose Fortran" repository that explores other alternatives like using a prototype like call get_arguments('-i 10 --help .false. --version .false. -A "a string"')
I picture implementing it would be like adding the contained routine into a main program at compilation time. A more general solution would be that any procedure that can convert the command line into a table of keywords and values could be passed to the routine, which could convert that to the NAMELIST group values. This would allow users to customize the parser.
@urbanjost you can use ``` to format your blocks of code and use other Markdown features to better format your post. See https://guides.github.com/features/mastering-markdown/ for how to do that.
The desired extension would allow for a NAMELIST to be flagged as an argument list and a new procedure called GET_ARGUMENTS([standard_name]). The expected behavior would look like
at a minimum only the NAMELIST and Sun CLIP standards would need supported, with developers free to support other command line styles.
The NAMELIST option can be simulated except for the ability to make the NAMELIST visible in a different scope (ie that the compiler can see the NAMELIST names).
The program can now be called using syntax like
The syntax for parsing the argument values would remain the same as for NAMELIST. That is, if reading an array or user-defined type you could use ARR=10,20.3,40 and so on, per NAMELIST rules, no matter what the syntax.
Other than arguing what formats would be standard and how (if at all) case is handled; the advantages would be
Fortran would have a standard command-line argument parsing interface.
Extremely easy to use using existing NAMELIST definition; and requiring no additional code to convert arguments to correct types by user.
Leveraging existing NAMELIST features non-existent options would be detected, variable names could be equivalenced, ...
The https://github.com/urbanjost/M_commandline site is as close as I could get to what I would like to because of the limitation on the scope of the NAMELIST group. It looks at ways long and short names and case could be handled and shows that existing Fortran features can almost be used to provide the feature, indicating this should be a relatively if somewhat system-dependent feature.
Considering the number of requests for a command-line interface I have seen I believe this is very commonly desired feature and using NAMELIST in this way would provide an easy-to-use interface. Far easier than any I have seen, including the C getopts(3c) routine.
In addition, since the arguments are in a NAMELIST format it would be trivial for the user to add config files to set defaults for their applications or to record and/or display the command line selections.
This specifically suggests changing NAMELIST groups, but has a lot in common with #25.
The closest I got to what I wanted that is also useful as a production version is the module M_CLI; but I only have tried it in one programming environment. If anyone tries it, I would appreciate hearing what compilers were used and any errors as I think it could be useful in the interim.