README.md 3.11 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

Arnaud Blanchard's avatar
Arnaud Blanchard committed
6
7
8
- 1. Parsing arguments
- 2. Interacting with the user in a terminal

Arnaud Blanchard's avatar
Arnaud Blanchard committed
9
10
11
12

Program main loop
=================

Arnaud Blanchard's avatar
Arnaud Blanchard committed
13
**BLC_COMMAND_LOOP**(period in micro seconds)
Arnaud Blanchard's avatar
Arnaud Blanchard committed
14
15
16

- Eventually acquires the profiling data.
- Check if it is the iteration limit and pauses in this case.
Arnaud Blanchard's avatar
Arnaud Blanchard committed
17
- Wait for the keyboard ( period == -1) or a waiting semaphores
Arnaud Blanchard's avatar
Arnaud Blanchard committed
18
- Ready to start, updates the timer
Arnaud Blanchard's avatar
Arnaud Blanchard committed
19
20

     {
Arnaud Blanchard's avatar
Arnaud Blanchard committed
21
       Your computation
Arnaud Blanchard's avatar
Arnaud Blanchard committed
22
23
     }

Arnaud Blanchard's avatar
Arnaud Blanchard committed
24
- Compute the time of the loop
Arnaud Blanchard's avatar
Arnaud Blanchard committed
25
- Post the enventual posting semaphores
Arnaud Blanchard's avatar
Arnaud Blanchard committed
26
- Eventually wait for more time (if it has been faster than the requested period )
Arnaud Blanchard's avatar
Arnaud Blanchard committed
27
28
29

Tutorial
========
Arnaud Blanchard's avatar
Arnaud Blanchard committed
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

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
92

Arnaud Blanchard's avatar
Arnaud Blanchard committed
93

Arnaud Blanchard's avatar
Arnaud Blanchard committed
94