libsim Versione 7.2.0
Tipi di dato | Funzioni/Subroutine | Variabili
Riferimenti per il modulo optionparser_class

Module for parsing command-line optons. Continua...

Tipi di dato

interface  delete
 Destructor for the optionparser class. Continua...
 
type  optionparser
 This class allows to parse the command-line options of a program in an object-oriented way, similarly to the optparse class found in Python library. Continua...
 
interface  optionparser_add
 Add a new option of a specific type. Continua...
 

Funzioni/Subroutine

type(optionparser) function, public optionparser_new (usage_msg, description_msg)
 Create a new instance of an optionparser object.
 
subroutine optionparser_add_c (this, short_opt, long_opt, dest, default, help, isopt)
 Add a new option with a character type argument.
 
subroutine optionparser_add_i (this, short_opt, long_opt, dest, default, help)
 Add a new option with an integer type argument.
 
subroutine optionparser_add_iarray (this, short_opt, long_opt, dest, default, help)
 Add a new option with an integer type array argument.
 
subroutine optionparser_add_r (this, short_opt, long_opt, dest, default, help)
 Add a new option with a real type argument.
 
subroutine optionparser_add_rarray (this, short_opt, long_opt, dest, default, help)
 Add a new option with a real type array argument.
 
subroutine optionparser_add_d (this, short_opt, long_opt, dest, default, help)
 Add a new option with a double precision type argument.
 
subroutine optionparser_add_darray (this, short_opt, long_opt, dest, default, help)
 Add a new option with a double precision type array argument.
 
subroutine optionparser_add_l (this, short_opt, long_opt, dest, help)
 Add a new logical option, without optional argument.
 
subroutine, public optionparser_add_count (this, short_opt, long_opt, dest, start, help)
 Add a new counter option, without optional argument.
 
subroutine, public optionparser_add_help (this, short_opt, long_opt, help)
 Add a new help option, with an optional argument.
 
subroutine, public optionparser_add_sep (this, help)
 Add a new separator option, with a text.
 
subroutine, public optionparser_parse (this, nextarg, status)
 This method performs the parsing of the command-line options which have been previously added using the optionparser_add family of methods.
 
subroutine, public optionparser_printhelp (this)
 Print on stdout a human-readable text representation of the help message.
 
subroutine optionparser_printhelptxt (this)
 Print on stdout a human-readable text representation of the help message.
 
subroutine optionparser_printhelpmd (this)
 Print on stdout a markdown representation of the help message.
 
subroutine optionparser_printhelphtmlform (this)
 Print on stdout an html form reflecting the command line options set up.
 
subroutine optionparser_make_completion (this)
 

Variabili

integer, parameter, public optionparser_ok = 0
 constants indicating the status returned by optionparser_parse, status of parsing: OK
 
integer, parameter, public optionparser_help = 1
 status of parsing: help has been requested
 
integer, parameter, public optionparser_err = 2
 status of parsing: an error was encountered
 

Descrizione dettagliata

Module for parsing command-line optons.

This module defines a class for parsing command-line arguments and generating help messages similar to the one found in the Python library.

This is an example of use:

! Copyright (C) 2010 ARPA-SIM <urpsim@smr.arpa.emr.it>
! authors:
! Davide Cesari <dcesari@arpa.emr.it>
! Paolo Patruno <ppatruno@arpa.emr.it>
! This program is free software; you can redistribute it and/or
! modify it under the terms of the GNU General Public License as
! published by the Free Software Foundation; either version 2 of
! the License, or (at your option) any later version.
! This program is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
! You should have received a copy of the GNU General Public License
! along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "config.h"
PROGRAM example_optionparser
IMPLICIT NONE
! option parsing
TYPE(optionparser) :: opt
INTEGER :: optind, optstatus
INTEGER :: i, iargc
! option variables
CHARACTER(len=80) :: name, extraopt
INTEGER :: nx
REAL :: xval, yval
DOUBLE PRECISION :: dval
TYPE(arrayof_integer) :: count_list
TYPE(arrayof_real) :: value_list
LOGICAL :: force, version
INTEGER :: verbose
! other variables
!INTEGER :: i
! define the option parser, for help2man usage_msg should start with
! "Usage:"
opt = optionparser_new(description_msg= &
'Example program for the optionparser class, &
&it does not do anything useful.', &
usage_msg='Usage: example_getopt [options] inputfile outputfile')
! add various options
CALL optionparser_add(opt, 'n', 'name', name, 'defaultname', help= &
'short and long character option with default value')
CALL optionparser_add(opt, '', 'nx', nx, 100, help= &
'long integer option with default value')
CALL optionparser_add(opt, 'x', 'xval', xval, 712., help= &
'short and long real option with default value')
yval = rmiss ! preset yval to recognize whether it has been set
CALL optionparser_add(opt, '', 'yval', yval, help= &
'long real option without default value')
CALL optionparser_add(opt, 'd', 'dval', dval, 489.0d0, help=&
'short and long double precision option with default value, &
&this should be a positive number')
CALL optionparser_add(opt, '', 'count-list', count_list, (/1,2,3/), help= &
'integer array option, a comma-separated list of values can be provided, &
&the default is partially displayed, in this case it is 1,2,3')
CALL optionparser_add(opt, '', 'value-list', value_list, help= &
'real array option, a comma-separated list of values can be provided, &
&the default in this case does not exist')
CALL optionparser_add(opt, 'f', 'force', force, help= &
'logical option, it cannot have a default value because it is .FALSE. by design')
CALL optionparser_add_count(opt, 'v', 'verbose', verbose, help= &
'count option without start value, it will be incremented at every appearence &
&of the option')
! help options, useful for help2man
CALL optionparser_add_help(opt, 'h', 'help', help='show an help message and exit')
CALL optionparser_add(opt, ' ', 'version', version, help='show version and exit')
! parse options and check for errors
CALL optionparser_parse(opt, optind, optstatus)
IF (optstatus == optionparser_help) THEN ! for help2man
CALL exit(0)
ELSE IF (optstatus == optionparser_err) THEN
CALL l4f_log(l4f_error,'in command-line parameters')
CALL raise_fatal_error()
ENDIF
IF (version) THEN ! for help2man
WRITE(*,'(A,1X,A)')'example_optionparser',version
CALL exit(0)
ENDIF
! check for specific errors in the options
IF (dval <= 0.0d0) THEN
CALL optionparser_printhelp(opt)
CALL l4f_log(l4f_error,'dval must be positive!')
CALL raise_fatal_error()
ENDIF
! release all the option data structure, the variables set by options will remain
CALL delete(opt)
! print a report of the options received
CALL l4f_log(l4f_info,'options report:')
CALL l4f_log(l4f_info,'name: '//trim(name))
CALL l4f_log(l4f_info,'nx: '//t2c(nx))
CALL l4f_log(l4f_info,'xval: '//t2c(xval))
IF (c_e(yval)) THEN
CALL l4f_log(l4f_info,'yval: '//t2c(yval))
ELSE
CALL l4f_log(l4f_info,'yval has not been specified')
ENDIF
CALL l4f_log(l4f_info,'dval: '//t2c(dval))
CALL l4f_log(l4f_info,'count-list: '//t2c(count_list%arraysize))
DO i = 1, count_list%arraysize
CALL l4f_log(l4f_info,t2c(i)//': '//t2c(count_list%array(i)))
ENDDO
CALL l4f_log(l4f_info,'value-list: '//t2c(value_list%arraysize))
DO i = 1, value_list%arraysize
CALL l4f_log(l4f_info,t2c(i)//': '//t2c(value_list%array(i)))
ENDDO
CALL l4f_log(l4f_info,'force: '//t2c(force))
CALL l4f_log(l4f_info,'verbose: '//t2c(verbose))
! parse the remaining optional arguments
IF (optind <= iargc()) THEN
CALL l4f_log(l4f_info, 'extra arguments provided:')
DO i = optind, iargc()
CALL getarg(i, extraopt)
CALL l4f_log(l4f_info, trim(extraopt))
ENDDO
ENDIF
CALL delete(count_list)
CALL delete(value_list)
END PROGRAM example_optionparser
Destructor for finalizing an array object.
Set of functions that return a trimmed CHARACTER representation of the input variable.
Function to check whether a value is missing or not.
Add a new option of a specific type.
This module defines usefull general purpose function and subroutine.
Utilities for CHARACTER variables.
Gestione degli errori.
classe per la gestione del logging
Definitions of constants and functions for working with missing values.
Module for parsing command-line optons.

Generated with Doxygen.