README.md 2.64 KB
Newer Older
Arnaud Blanchard's avatar
Arnaud Blanchard committed
1
2
3
BLC program
===========

Arnaud Blanchard's avatar
Arnaud Blanchard committed
4
Library providing command line program facilities
Arnaud Blanchard's avatar
Arnaud Blanchard committed
5
6

- Parsing arguments
Arnaud Blanchard's avatar
Arnaud Blanchard committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
- 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'
```



Arnaud Blanchard's avatar
Arnaud Blanchard committed
73

Arnaud Blanchard's avatar
Arnaud Blanchard committed
74

Arnaud Blanchard's avatar
Arnaud Blanchard committed
75