项目作者: szaghi

项目描述 :
Fortran command Line Arguments Parser for poor people
高级语言: Fortran
项目地址: git://github.com/szaghi/FLAP.git
创建时间: 2014-04-08T14:56:56Z
项目社区:https://github.com/szaghi/FLAP

开源协议:

下载


FLAP GitHub tag Join the chat at https://gitter.im/szaghi/FLAP

License
License
License
License

Status
CI Status
Coverage Status

FLAP, Fortran command Line Arguments Parser for poor people

A KISS pure Fortran Library for building powerful, easy-to-use, elegant command line interfaces

  • FLAP is a pure Fortran (KISS) library for building easily nice Command Line Interfaces (CLI) for modern Fortran projects;
  • FLAP is Fortran 2003+ standard compliant;
  • FLAP is OOP designed;
  • FLAP is a Free, Open Source Project.

Issues

GitHub issues

Compiler Support

Compiler
Compiler
Compiler
Compiler
Compiler
Compiler


| What is FLAP? | Main features | Copyrights | Documentation | Install |


What is FLAP?

Modern Fortran standards (2003+) have introduced support for Command Line Arguments (CLA), thus it is possible to construct nice and effective Command Line Interfaces (CLI). FLAP is a small library designed to simplify the (repetitive) construction of complicated CLI in pure Fortran (standard 2003+). FLAP has been inspired by the python module argparse trying to mimic it. Once you have defined the arguments that are required by means of a user-friendly method of the CLI, FLAP will parse the CLAs for you. It is worthy of note that FLAP, as argparse, also automatically generates help and usage messages and issues errors when users give the program invalid arguments.

Go to Top

Main features

FLAP is inspired by the python great module argparse, thus many features are taken from it. Here the main features are listed.

  • User-friendly methods for building flexible and effective Command Line Interfaces (CLI);
  • comprehensive Command Line Arguments (CLA) support:
    • support optional and non optional CLA;
    • support boolean CLA;
    • support positional CLA;
    • support list of allowable values for defined CLA with automatic consistency check;
    • support multiple valued (list of values, aka list-valued) CLA:
      • compiletime sized list, e.g. nargs='3';
      • runtime sized list with at least 1 value, e.g. nargs='+';
      • runtime sized list with any size, even empty, e.g. nargs='*';
    • support mutually exclusive CLAs;
    • self-consistency-check of CLA definition;
    • support fake CLAs input from a string;
    • support fake CLAs input from environment variables;
  • comprehensive command (group of CLAs) support:
    • support nested subcommands;
    • support mutually exclusive commands;
    • self-consistency-check of command definition;
  • automatic generation of help and usage messages;
  • consistency-check of whole CLI definition;
  • errors trapping for invalid CLI usage;
  • POSIX style compliant;
  • automatic generation of MAN PAGE using your CLI definition!;
  • replicate all the useful features of argparse;
  • implement docopt features.
  • implement click features.

Any feature request is welcome.

Go to Top

Copyrights

FLAP is an open source project, it is distributed under a multi-licensing system:

Anyone is interest to use, to develop or to contribute to FLAP is welcome, feel free to select the license that best matches your soul!

More details can be found on wiki.

Go to Top

Documentation

Besides this README file the FLAP documentation is contained into its own wiki. Detailed documentation of the API is contained into the GitHub Pages that can also be created locally by means of ford tool.

A Taste of FLAP

A minimal plate:

  1. program minimal
  2. type(command_line_interface) :: cli ! Command Line Interface (CLI).
  3. character(99) :: string ! String value.
  4. integer :: error ! Error trapping flag.
  5. call cli%init(description = 'minimal FLAP example')
  6. call cli%add(switch='--string', &
  7. switch_ab='-s', &
  8. help='a string', &
  9. required=.true., &
  10. act='store', &
  11. error=error)
  12. if (error/=0) stop
  13. call cli%get(switch='-s', val=string, error=error)
  14. if (error/=0) stop
  15. print '(A)', cli%progname//' has been called with the following argument:'
  16. print '(A)', 'String = '//trim(adjustl(string))
  17. endprogram minimal

That built and run provides:

  1. ./minimal
  2. ./minimal: error: named option "--string" is required!
  3. usage: ./exe/test_minimal --string value [--help] [--version]
  4. minimal FLAP example
  5. Required switches:
  6. --string value, -s value
  7. a string
  8. Optional switches:
  9. --help, -h
  10. Print this help message
  11. --version, -v
  12. Print version

A nice automatic help-message, right? Executed correctly gives.

  1. ./minimal --string 'hello world'
  2. ./exe/minimal has been called with the following argument:
  3. String = hello world

For more details, see the provided tests.

Nested (sub)commands

FLAP fully supports nested (sub)commands or groups of command line arguments. For example a fake git toy remake can be coded as

  1. ! initializing Command Line Interface
  2. call cli%init(progname = 'test_nested', &
  3. version = 'v2.1.5', &
  4. authors = 'Stefano Zaghi', &
  5. license = 'MIT', &
  6. description = 'Toy program for testing FLAP with nested commands',&
  7. examples = ['test_nested ',&
  8. 'test_nested -h ',&
  9. 'test_nested init ',&
  10. 'test_nested commit -m "fix bug-1"',&
  11. 'test_nested tag -a "v2.1.5" '])
  12. ! set a Command Line Argument without a group to trigger authors names printing
  13. call cli%add(switch='--authors',switch_ab='-a',help='Print authors names',required=.false.,act='store_true',def='.false.')
  14. ! set Command Line Arguments Groups, i.e. commands
  15. call cli%add_group(group='init',description='fake init versioning')
  16. call cli%add_group(group='commit',description='fake commit changes to current branch')
  17. call cli%add_group(group='tag',description='fake tag current commit')
  18. ! set Command Line Arguments of commit command
  19. call cli%add(group='commit',switch='--message',switch_ab='-m',help='Commit message',required=.false.,act='store',def='')
  20. ! set Command Line Arguments of commit command
  21. call cli%add(group='tag',switch='--annotate',switch_ab='-a',help='Tag annotation',required=.false.,act='store',def='')
  22. ! parsing Command Line Interface
  23. call cli%parse(error=error)
  24. if (error/=0) then
  25. print '(A)', 'Error code: '//trim(str(n=error))
  26. stop
  27. endif
  28. ! using Command Line Interface data to trigger program behaviour
  29. call cli%get(switch='-a',val=authors_print,error=error) ; if (error/=0) stop
  30. if (authors_print) then
  31. print '(A)','Authors: '//cli%authors
  32. elseif (cli%run_command('init')) then
  33. print '(A)','init (fake) versioning'
  34. elseif (cli%run_command('commit')) then
  35. call cli%get(group='commit',switch='-m',val=message,error=error) ; if (error/=0) stop
  36. print '(A)','commit changes to current branch with message "'//trim(message)//'"'
  37. elseif (cli%run_command('tag')) then
  38. call cli%get(group='tag',switch='-a',val=message,error=error) ; if (error/=0) stop
  39. print '(A)','tag current branch with message "'//trim(message)//'"'
  40. else
  41. print '(A)','cowardly you are doing nothing... try at least "-h" option!'
  42. endif

that when invoked without arguments prompts:

  1. cowardly you are doing nothing... try at least "-h" option!

and invoked with -h option gives:

  1. usage: test_nested [--authors] [--help] [--version] {init,commit,tag} ...
  2. Toy program for testing FLAP with nested commands
  3. Optional switches:
  4. --authors, -a
  5. default value .false.
  6. Print authors names
  7. --help, -h
  8. Print this help message
  9. --version, -v
  10. Print version
  11. Commands:
  12. init
  13. fake init versioning
  14. commit
  15. fake commit changes to current branch
  16. tag
  17. fake tag current commit
  18. For more detailed commands help try:
  19. test_nested init -h,--help
  20. test_nested commit -h,--help
  21. test_nested tag -h,--help
  22. Examples:
  23. test_nested
  24. test_nested -h
  25. test_nested init
  26. test_nested commit -m "fix bug-1"
  27. test_nested tag -a "v2.1.5"

For more details, see the provided example.

Go to Top


Install

FLAP is a Fortran library composed by several modules.

Before download and compile the library you must check the requirements.

To download and build the project two main ways are available:


Go to Top

install script

FLAP ships a bash script (downloadable from here) that is able to automatize the download and build steps. The script install.sh has the following usage:

  1. ./install.sh
  2. Install script of FLAP
  3. Usage:
  4. install.sh --help|-?
  5. Print this usage output and exit
  6. install.sh --download|-d <arg> [--verbose|-v]
  7. Download the project
  8. --download|-d [arg] Download the project, arg=git|wget to download with git or wget respectively
  9. --verbose|-v Output verbose mode activation
  10. install.sh --build|-b <arg> [--verbose|-v]
  11. Build the project
  12. --build|-b [arg] Build the project, arg=fobis|make|cmake to build with FoBiS.py, GNU Make or CMake respectively
  13. --verbose|-v Output verbose mode activation
  14. Examples:
  15. install.sh --download git
  16. install.sh --build make
  17. install.sh --download wget --build cmake

The script does not cover all possibilities.

The script operation modes are 2 (collapsible into one-single-mode):

you can mix any of the above combinations accordingly to the tools available.

Typical usages are:

  1. # download and prepare the project by means of git and build with GNU Make
  2. install.sh --dowload git --build make
  3. # download and prepare the project by means of wget (curl) and build with CMake
  4. install.sh --dowload wget --build cmake
  5. # download and prepare the project by means of git and build with FoBiS.py
  6. install.sh --dowload git --build fobis

Go to Top

manually download and build

download

To download all the available releases and utilities (fobos, license, readme, etc…), it can be convenient to clone whole the project:

  1. git clone https://github.com/szaghi/FLAP
  2. cd FLAP
  3. git submodule update --init

Alternatively, you can directly download a release from GitHub server, see the ChangeLog.

build

The most easy way to compile FLAP is to use FoBiS.py within the provided fobos file.

Consequently, it is strongly encouraged to install FoBiS.py.

| Build by means of FoBiS | Build by means of GNU Make | Build by means of CMake |


build by means of FoBiS

FoBiS.py is a KISS tool for automatic building of modern Fortran projects. Providing very few options, FoBiS.py is able to build almost automatically complex Fortran projects with cumbersome inter-modules dependency. This removes the necessity to write complex makefile. Moreover, providing a very simple options file (in the FoBiS.py nomenclature indicated as fobos file) FoBiS.py can substitute the (ab)use of makefile for other project stuffs (build documentations, make project archive, etc…). FLAP is shipped with a fobos file that can build the library in both static and shared forms and also build the Test_Driver program. The provided fobos file has several building modes.

listing fobos building modes

Typing:

  1. FoBiS.py build -lmodes

the following message should be printed:

  1. The fobos file defines the following modes:
  2. - "shared-gnu"
  3. - "static-gnu"
  4. - "test-driver-gnu"
  5. - "shared-gnu-debug"
  6. - "static-gnu-debug"
  7. - "test-driver-gnu-debug"
  8. - "shared-intel"
  9. - "static-intel"
  10. - "test-driver-intel"
  11. - "shared-intel-debug"
  12. - "static-intel-debug"
  13. - "test-driver-intel-debug"

The modes should be self-explicative: shared, static and test-driver are the modes for building (in release, optimized form) the shared and static versions of the library and the Test Driver program, respectively. The other 3 modes are the same, but in debug form instead of release one. -gnu use the GNU gfortran compiler while -intel the Intel one.

building the library

The shared or static directories are created accordingly to the form of the library built. The compiled objects and mod files are placed inside this directory, as well as the linked library.

release shared library
  1. FoBiS.py build -mode shared-gnu
release static library
  1. FoBiS.py build -mode static-gnu
debug shared library
  1. FoBiS.py build -mode shared-gnu-debug
debug static library
  1. FoBiS.py build -mode static-gnu-debug
building the Test Driver program

The Test_Driver directory is created. The compiled objects and mod files are placed inside this directory, as well as the linked program.

release test driver program
  1. FoBiS.py build -mode test-driver-gnu
debug test driver program
  1. FoBiS.py build -mode test-driver-gnu-debug
listing fobos rules

Typing:

  1. FoBiS.py rule -ls

the following message should be printed:

  1. The fobos file defines the following rules:
  2. - "makedoc" Rule for building documentation from source files
  3. Command => rm -rf doc/html/*
  4. Command => ford doc/main_page.md
  5. Command => cp -r doc/html/publish/* doc/html/
  6. - "deldoc" Rule for deleting documentation
  7. Command => rm -rf doc/html/*
  8. - "maketar" Rule for making tar archive of the project
  9. Command => tar -czf FLAP.tar.gz *
  10. - "makecoverage" Rule for performing coverage analysis
  11. Command => FoBiS.py clean -mode test-driver-gnu
  12. Command => FoBiS.py build -mode test-driver-gnu -coverage
  13. Command => ./Test_Driver/Test_Driver
  14. Command => ./Test_Driver/Test_Driver -v
  15. Command => ./Test_Driver/Test_Driver -s 'Hello FLAP' -i 2
  16. Command => ./Test_Driver/Test_Driver 33.0 -s 'Hello FLAP' --integer_list 10 -3 87 -i 3 -r 64.123d0 --boolean --boolean_val .false.
  17. - "coverage-analysis" Rule for performing coverage analysis and saving reports in markdown
  18. Command => FoBiS.py clean -mode test-driver-gnu
  19. Command => FoBiS.py build -mode test-driver-gnu -coverage
  20. Command => ./Test_Driver/Test_Driver
  21. Command => ./Test_Driver/Test_Driver -v
  22. Command => ./Test_Driver/Test_Driver -s 'Hello FLAP' -i 2
  23. Command => ./Test_Driver/Test_Driver 33.0 -s 'Hello FLAP' --integer_list 10 -3 87 -i 3 -r 64.123d0 --boolean --boolean_val .false.
  24. Command => gcov -o Test_Driver/obj/ src/*
  25. Command => FoBiS.py rule -gcov_analyzer wiki/ Coverage-Analysis
  26. Command => rm -f *.gcov

The rules should be self-explicative.


build by means of GNU Make

Bad choice :-)

However, a makefile (generated by FoBiS.py…) to be used with a compatible GNU Make tool is provided.

It is convenient to clone the whole FLAP repository and run a standard make:

  1. git clone https://github.com/szaghi/FLAP
  2. cd FLAP
  3. make -j 1

This commands build all tests (executables are in exe/ directory). To build only the library (statically linked) type:

  1. git clone https://github.com/szaghi/FLAP
  2. cd FLAP
  3. make -j 1 STATIC=yes

Build by means of CMake

Bad choice :-)

However, a CMake setup (kindly developed by victorsndvg) is provided.

It is convenient to clone the whole FLAP repository and run a standard CMake configure/build commands:

  1. git clone https://github.com/szaghi/FLAP $YOUR_FLAP_PATH
  2. mkdir build
  3. cd build
  4. cmake $YOUR_FLAP_PATH
  5. cmake --build .

If you want to run the tests suite type:

  1. git clone https://github.com/szaghi/FLAP $YOUR_FLAP_PATH
  2. mkdir build
  3. cd build
  4. cmake -DFLAP_ENABLE_TESTS=ON $YOUR_FLAP_PATH
  5. cmake --build .
  6. ctest

Build by means of FPM

A Fortran Package Manager manifest file is also included, so that the library and test cases can be compiled with FPM. For example:

  1. fpm build --profile release
  2. fpm test --profile release

To use FLAP within your fpm project, add the following to your fpm.toml file:

  1. [dependencies]
  2. FLAP = { git="https://github.com/szaghi/FLAP.git" }

Or, to use a specific revision:

  1. [dependencies]
  2. FLAP = { git="https://github.com/szaghi/FLAP.git", rev = "11cb276228d678c1d9ce755badf0ce82094b0852" }

Note that, when compiling with FPM, the git submodules in the src/third_party directory are not used, but FPM will download these separately, based on the versions specified in the fpm.toml file.

Go to Top

NVFortran Compiling Issue

Thanks to Carl Ponder (@cponder), we know that Nvidia NVFortran compiler misunderstand a quoted string containing a backslash into source files. To overcome this issue we suggest to use the -Mbackslash switch of NVFortran compiler.

For example, to compile by means of CMake use:

  1. git clone https://github.com/szaghi/FLAP $YOUR_FLAP_PATH
  2. mkdir build
  3. cd build
  4. cmake -D CMAKE_Fortran_FLAGS="-Mbackslash" $YOUR_FLAP_PATH
  5. cmake --build .

Similarly, into the fobos config file there is a specific template for NVFortran where this switch has been used to compile FLAP by means of Nvidia compiler. To test it use:

  1. FoBiS.py build -mode static-nvf

Go to Top