Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
blaar
blibs
blc_program
Commits
e0117410
Commit
e0117410
authored
Apr 22, 2018
by
Arnaud Blanchard
Browse files
Update README.md
parent
8a20cfbc
Changes
1
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
e0117410
BLC program
===========
Copyright :
[
ETIS
](
http://www.etis.ensea.fr/neurocyber
)
- ENSEA, University of Cergy-Pontoise, CNRS (2011-2016)
Author :
[
Arnaud Blanchard
](
http://arnaudblanchard.thoughtsheet.com
)
Licence :
[
CeCILL v2.1
](
http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
)
Library providing command line program facilities
### Library providing command line program facilities
-
Parsing arguments
-
Interacting with the user
-
Interacting with the user in a terminal
BLC program
============
Exemple based on **t_parse_arguments**
------------------------------------------------
code
```
c++
#include
"blc_program.h"
int
main
(
int
argc
,
char
**
argv
){
char
const
*
text_option
,
*
first_parameter
,
*
optional_parameter
,
*
flag
;
//The text that appear when help is called.
blc_program_set_description
(
"Program to show how to parse arguments."
);
//Define the options to be parsed with 'blc_program_option_interpret'.
blc_program_add_option
(
&
flag
,
'f'
,
"flag"
,
NULL
,
"Show how to read a flag"
,
NULL
);
blc_program_add_option
(
&
text_option
,
's'
,
"simple"
,
"string"
,
"Simple text as option"
,
"Default text"
);
//Require one argument.
blc_program_add_parameter
(
&
first_parameter
,
"string"
,
1
,
"Required parameter "
,
NULL
);
//Accept a second optional argument.
blc_program_add_parameter
(
&
optional_parameter
,
"string"
,
0
,
"Show how to accept simple text as option"
,
NULL
);
//Interprets the arguments, associates variables and print program name as title.
blc_program_option_interpret_and_print_title
(
&
argc
,
&
argv
);
//We display on stderr. It is a good habit to use for interaction with the user
fprintf
(
stderr
,
"Display help:
\n
"
);
//Display all the possible options / arguments
blc_program_option_display_help
();
//After interpret options, we can use the variable **blc_program_name**
fprintf
(
stderr
,
"Parsed arguments:
\n
"
);
//We present the parsed variables
fprintf
(
stderr
,
"- The simple option text is: '%s'
\n
"
,
text_option
);
if
(
flag
)
fprintf
(
stderr
,
"- The flag is activated, its content (usually useless) is : '%s'
\n
"
,
flag
);
else
fprintf
(
stderr
,
"- The flag is not activated.
\n
"
);
fprintf
(
stderr
,
"- The first argument is: '%s'
\n
"
,
first_parameter
);
if
(
optional_parameter
)
fprintf
(
stderr
,
"- The optional argument is: '%s'
\n
"
,
optional_parameter
);
else
fprintf
(
stderr
,
"- No optional argument
\n
"
);
return
0
;
}
```
Execute:
`../run.sh t_parse_arguments arg1 arg2`
Result
```
Display help:
usage: t_parse_args [-f] [-s string] string [string]
Program to show how to parse arguments.
positional arguments:
string Required parameter string Show how to accept simple text as option
optional arguments:
-f, --flag Show how to read a flag
-s, --simple string Simple text as option (default: Default text)
Parsed arguments:
- The simple option text is: 'Default text'
- The flag is not activated.
- The first argument is: 'arg1'
- The optional argument is: 'arg2'
```
### [Online documentation](https://framagit.org/blaar/blc_program/wikis/home)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment