Skip to content

Commit 8b18bc9

Browse files
committed
Minor refactoring, edit documentation
1 parent aa07ead commit 8b18bc9

File tree

2 files changed

+171
-68
lines changed

2 files changed

+171
-68
lines changed

README.md

Lines changed: 89 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,111 @@
1-
f90getopt
2-
=========
1+
# f90getopt [![Status](https://img.shields.io/badge/status-stable-brightgreen.svg)]()
32

4-
getopt()- and getopt_long()-like functionality (similar to the C-functions) for Fortran 90. Based on sources from [Mark Gates](http://lagrange.mechse.illinois.edu/mwest/partmc/partmc-2.2.1/src/getopt.F90).
3+
getopt()- and getopt_long()-like functionality (similar to the C-functions) for Fortran 90/2003 or higher. Based on sources from [Mark Gates](http://lagrange.mechse.illinois.edu/mwest/partmc/partmc-2.2.1/src/getopt.F90).
54

6-
## Purpose:
5+
*f90getopt* is developed as an easy to learn and compact library in one source file. The `f90getopt.F90` file can just be added to existing sources and deployed with them without producing dependencies. You can "learn" f90getopt in minutes and therefore it is even suitable for very small projects or "throw-away-code".
6+
7+
* [Purpose](#Purpose)
8+
* [Features](#Features)
9+
* [Requirements](#Requirements)
10+
* [Example](#Example)
11+
* [Build the sample program](#Build-the-sample-program)
12+
* [Run the sample program](#Run-the-sample-program)
13+
* [Compile and link to static library](#Compile-and-link-to-static-library)
14+
* [Userfunctions and Variables](#Userfunctions-and-Variables)
15+
* [Differences](#Differences)
16+
* [From C version](#From-C-version)
17+
* [For long options](#For-long-options)
18+
* [Changelog](#Changelog)
19+
* [License](#License)
20+
21+
## Purpose
722

823
Parsing command-line options and arguments like:
924

1025
<pre>myapp -xv --longarg --arg=5.0 -p 9</pre>
1126

12-
## Features:
27+
## Features
1328

1429
* Short option without argument (e.g.: -x)
1530
* Short option with argument (e.g.: -p 9 or -p9)
1631
* Short options w/o arguments can be embraced (e.g.: -xv)
1732
* Long option w/o argument (e.g: --longarg)
18-
* Long option w/ argument (e.g.: --arg 5.0) and ***NEW***: Equal sign (e.g.: --arg=5.0)
33+
* Long option w/ argument (e.g.: --arg 5.0 or --arg=5.0)
1934

2035
## Requirements
21-
Fortran 90 compiler which offers Fortran 2003 features *command_argument_count()* and *get_command_argument()*. E.g.: gfortran (GNU), g95 (g95.org), ifort (Intel), etc.
36+
37+
Fortran 2003 or Fortran 90 compiler which offers Fortran 2003 features *command_argument_count()* and *get_command_argument()*. E.g.: gfortran (GNU), g95 (g95.org), ifort (Intel), etc.
2238

2339
## Example
2440

41+
This is a full working example and it make use of long and short options. It is well documented and should answer most questions. If you need further information, refer the [Wiki page](https://github.com/haniibrahim/f90getopt/wiki)
42+
2543
```
2644
program f90getopt_sample
45+
! Sample program for f90getopt function
46+
2747
use f90getopt
2848
implicit none
29-
!character:: ch ! Unused: ch=getopt()
3049
31-
! START For longopts only
32-
type(option_s):: opts(3)
33-
opts(1) = option_s( "alpha", .false., 'a' )
34-
opts(2) = option_s( "beta", .true., 'b' )
35-
opts(3) = option_s( "help", .false., 'h')
50+
! START for longopts only (optional)
51+
! ----------------------------------
52+
! option_s derived type:
53+
! 1st value = long option name (character array, max. 80)
54+
! 2nd value = if option has value (boolean)
55+
! 3rd value = short option name (single character), same as in getopt()
56+
! option_s is not needed if you just use short options
57+
type(option_s) :: opts(3)
58+
opts(1) = option_s("alpha", .false., "a")
59+
opts(2) = option_s("beta", .true., "b")
60+
opts(3) = option_s("help", .false., "h")
3661
! END longopts
3762
63+
3864
! If no options were committed
39-
if (command_argument_count() .eq. 0 ) then
40-
write (*,*) "Available Options: -a. --alpha -b X --beta=X --beta X"
65+
! ----------------------------
66+
if (command_argument_count() .eq. 0) then
67+
print*, "ERROR: Program has options: -a. --alpha -b x --beta=x --beta x"
4168
end if
4269
43-
! Process options one by one
70+
71+
! START Processing options
72+
! ------------------------
73+
! Process short options one by one and long options if specified in option_s
74+
!
75+
! getopt(optstr, longopt):
76+
! - optstr = character of short option character without a space
77+
! ":" after a character says that this option requires a value
78+
! - opts = longopts, if specified in option_s (optional)
4479
do
45-
select case( getopt( "ahb:", opts ) ) ! opts is optional (for longopts only)
46-
case( char(0) )
80+
select case(getopt("ab:h", opts))
81+
case(char(0))
4782
exit
48-
case( 'a' )
49-
print *, 'option alpha/a'
50-
case( 'b' )
51-
print *, 'option beta/b=', optarg
52-
case( 'h' )
53-
print *, 'help-screen'
83+
case("a")
84+
print*, "option alpha/a"
85+
case("b")
86+
print*, "option beta/b=", trim(optarg) ! "trim" is quite useful to avoid trailing spaces
87+
case("h")
88+
print*, "help-screen"
5489
end select
5590
end do
91+
! END processing options
92+
5693
end program f90getopt_sample
5794
```
5895

59-
Build the sample program:
96+
### Build the sample program
6097

61-
Put ```f90getopt.f90``` and your sample program from above (let's say ```f90getopt-sample.f90```) in the same directory and change to this directory with the ```cd```command in the terminal (or Windows command prompt). Then type:
98+
Put `f90getopt.f90` and your sample program from above (let's say `f90getopt_sample.f90`) in the same directory and change to this directory with the `cd` command in the terminal (or Windows command prompt). Then type:
6299

63100
```
64101
gfortran f90getopt.F90 f90getopt_sample.f90 -o f90getopt_sample
65102
```
66103

67-
(you can omit `.exe` in `-o f90fetopt_sample` on Windows)
104+
(you can omit `.exe` in `-o f90getopt_sample` on Windows)
105+
106+
to compile it.
68107

69-
to compile it and run it with, e.g.:
108+
### Run the sample program
70109

71110
On Unices:
72111

@@ -89,19 +128,19 @@ Output is:
89128
option beta/b=23.2
90129
```
91130

92-
## Compile and link to (static) library
131+
## Compile and link to static library
93132

94133
To avoid copying the source file `f90getopt.F90` to your working directory everytime you want to use their features, it make sense to create a static library. After, you just need to link the library to your project. To do this follow the steps below:
95134

96-
Change to the directory where the ```f90getopt.F90``` is located and type:
135+
Change to the directory where the `f90getopt.F90` is located and type:
97136

98137
```
99138
gfortran -c f90getopt.F90
100139
ar cr libf90getopt.a f90getopt.o
101140
ranlib libf90getopt.a
102141
```
103142

104-
You will get a static libray called ```libf90getopt.a``` and a module file ```f90getopt.mod```. Move the a-file on UNIX(-like) systems to ```/usr/local/lib``` and the mod-file to ```/usr/local/include```:
143+
You will get a static libray called `libf90getopt.a` and a module file `f90getopt.mod`. Move the a-file on UNIX(-like) systems to `/usr/local/lib` and the mod-file to `/usr/local/include`:
105144

106145
```
107146
sudo cp ./libf90getopt.a /usr/local/lib/
@@ -113,51 +152,56 @@ On Windows go to the appropriate directory of your compiler-system. MinGW and Cy
113152
To compile and link the sample program with the libray can be done on Unices with:
114153

115154
```
116-
gfortran -o f90getopt-sample f90getopt-sample.f90 -I/usr/local/include -lf90getopt
155+
gfortran -o f90getopt_sample f90getopt_sample.f90 -I/usr/local/include -lf90getopt
117156
```
118157

119158
Change paths to match the right ones on Windows.
120159

121-
## Userfunction
160+
## Userfunctions and Variables
161+
122162
| Type | Name | Brief Description |
123163
| --------------- | ------------------------------------ | ------------------------------------------------------------------------------------------------------------------ |
124-
| function | getopt(shortopts-string, [longopts]) | Returns short option character & value (if applicable) of all arguments one by one |
125-
| global variable | optarg | value of the current option, e.g. "3" when long argument = --num=3. |
126-
| global variable | optopt | Option (single) character e.g. "b" when option = -b |
164+
| Function | getopt(shortopts-string, [longopts]) | Returns short option character & value (if applicable) of all arguments one by one |
165+
| Derived Type | option_s(longopt, value, shortopt) | Contains the option's long name, if value is required and option's short (character) |
166+
127167
Refer example code above for details.
128168

129-
## Differences ...
130-
### ... from C version
169+
## Differences
170+
171+
### From C version
172+
131173
- when options are finished, C version returns -1 instead of char(0), and thus stupidly requires an int instead of a char.
132174
- does not support optreset
133175
- does not support "--" as last argument
134176
- if no argument, optarg is blank, not NULL
135177
- argc and argv are implicit
136178

137-
### ... for long options
179+
### For long options
180+
138181
- optional argument to getopt(), rather than separate function getopt_long()
139182
- has_arg is logical, and does not support optional_argument
140183
- does not support flag field (and thus always returns val)
141184
- does not support longindex
142185
- knows the length of longopts, so does not need an empty last record
143186

144-
## Changelog
187+
## Changelog
145188

146189
| Version | Description |
147190
| ------- | ---------------------------------------------------------------------------------------------------------------------- |
148191
| 0.9.0 | Almost finished. Equal sign recognition in longoption (logopt=arg) is missing for v1.0 |
149192
| 1.0.0 | Added support for equal sign (=) in long options, like --beta=2.0. Error messages are displayed on stderr (not stdout) |
150-
| 1.0.1 | Longopt bug fixed and refactoring. |
151-
| 1.0.2 | Bug in README.md fixed |
152-
|1.0.3 | Bug in README.md (sample code section) fixed |
193+
| 1.0.1 | Longopt bug fixed and refactoring. |
194+
| 1.0.2 | Bug in README.md fixed |
195+
| 1.0.3 | Bug in README.md (sample code section) fixed |
196+
| 1.0.4 | Portable declaration of stdin/out/err fixed, minor refactoring and documentation, => GPL 3.0, Wiki page |
153197

154198
## License
155199

156-
**GNU Public License v. 2.0**
200+
[![License](https://img.shields.io/badge/license-GNU%20GeneraL%20Public%20License%20v3%20,%20GPLv3-blue.svg)]()
157201

158202
Copyright (C) 2014 Hani Andreas Ibrahim
159203

160-
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.
204+
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 3 of the License, or (at your option) any later version.
161205

162206
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.
163207

0 commit comments

Comments
 (0)