Skip to content

Commit

Permalink
fprettify && multi-line help message
Browse files Browse the repository at this point in the history
  • Loading branch information
0382 committed Jan 31, 2023
1 parent a60c678 commit 0c287ed
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 226 deletions.
38 changes: 23 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Modern Fortran command line parser, implemented with OOP.
## Example
```fortran
program test
use iso_fortran_env, only : stdout=>output_unit
use iso_fortran_env, only: stdout => output_unit
use argparse
implicit none
character(len=10), parameter :: program_name = "qcalc"
Expand All @@ -16,17 +16,17 @@ program test
call args%add_sc_option("-v", "--version", "show version info", show_version_info)
call args%add_option_logical("-o", "--openmp", "use openmp or not") ! logical option has no default
call args%add_option_logical("-m", "--mpi", "use mpi or not")
call args%add_option_integer("-t", "--thread", "thread number, valid if openmp is set", 1)
call args%add_option_integer("-p", "--process", "process number, valid if mpi is set", 1)
call args%add_option_integer("-t", "--thread", "thread number,\nit is valid only if openmp is set", 1)
call args%add_option_integer("-p", "--process", "process number,\nit is valid only if mpi is set", 1)
call args%add_option_string("", "--chemical", "chemical formula", "H2O") ! short name can be empty
call args%add_named_argument_string("input", "initialize file")
call args%add_named_argument_string("output", "output file")
call args%parse()
if(args%has_option("--openmp")) then
if (args%has_option("--openmp")) then
print '(A,I2,A)', "openmp is used, and we use ", args%get_option_integer("-t"), " threads"
end if
if(args%has_option("--mpi")) then
if (args%has_option("--mpi")) then
print '(A,I2,A)', "mpi is used, and we use ", args%get_option_integer("-p"), " processes"
end if
print '(A,A)', "the calculated chemical is ", trim(args%get_option_string("--chemical"))
Expand All @@ -36,7 +36,7 @@ program test
! you can also print the cached `args` into INI file
! print '(/,A)', "All of the options and arguments are shown below"
! call args%print_as_ini(stdout, .true.)
contains
contains
subroutine show_version_info
print "(A)", trim(program_name)//" version 0.1.0"
end subroutine show_version_info
Expand All @@ -50,17 +50,19 @@ usage: qcalc [options] [=input] [=output]
A quantum physics calculation program.

Options:
-?, --help show this help message
-v, --version show version info
-o, --openmp use openmp or not
-m, --mpi use mpi or not
-t, --thread (integer) thread number, valid if openmp is set
-p, --process (integer) process number, valid if mpi is set
--chemical (string) chemical formula
-?, --help show this help message
-v, --version show version info
-o, --openmp use openmp or not
-m, --mpi use mpi or not
-t, --thread (integer) thread number,
it is valid only if openmp is set
-p, --process (integer) process number,
it is valid only if mpi is set
--chemical (string) chemical formula

Named arguments:
input (string) initialize file
output (string) output file
input (string) initialize file
output (string) output file
> qclac -o -t 4 input=input.txt output=out.bin
openmp is used, and we use 4 threads
the calculated chemical is H2O
Expand Down Expand Up @@ -178,13 +180,19 @@ subroutine print_help
end subroutine
```
### multi-line help message
Some times, help message may be very long. You can use `\n` to as newline split mark. Of course, Fortran does not have escape characters. I just use some character split technique to realize this feature.
### print argparser
You can print the argparser into INI file format. If the second dummy argument is set to `.true.`, then print help message as comments.
```fortran
call args%print_as_ini(stdout, .true.)
```
### `print_uasge` && `set_program_name`
If you give none command line argument, the program will call `print_usage` and exit. It is just the first line of `print_help`. `set_program_name` only affects program name in `print_usage`, if you does not set it, it will use `argv[0]`.
## Reference
This package works like my c++ package [argparse](https://github.com/0382/util/tree/main/cpp/argparse). They are imspired by c++ package [cmdline](https://github.com/tanakh/cmdline) and python's standard library package [argparse](https://docs.python.org/3/library/argparse.html).
Loading

0 comments on commit 0c287ed

Please sign in to comment.