B

blc_program

Basic library for creating command line program

BLC program

Library providing command line program facilities

  • 1. Parsing arguments
  • 2. Interacting with the user in a terminal

Program main loop

BLC_COMMAND_LOOP(period in micro seconds)

  • Eventually acquires the profiling data.
  • Check if it is the iteration limit and pauses in this case.
  • Wait for the keyboard ( period == -1) or a waiting semaphores
  • Ready to start, updates the timer

    { Your computation }

  • Compute the time of the loop

  • Post the enventual posting semaphores

  • Eventually wait for more time (if it has been faster than the requested period )

Tutorial

Exemple based on t_parse_arguments

code

#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'