OpenCores
URL https://opencores.org/ocsvn/sqmusic/sqmusic/trunk

Subversion Repositories sqmusic

[/] [sqmusic/] [trunk/] [cpp/] [args.h] - Blame information for rev 16

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 16 gryzor
/*
2
  (c) Jose Tejada Gomez, 9th May 2013
3
  You can use this file following the GNU GENERAL PUBLIC LICENSE version 3
4
  Read the details of the license in:
5
  http://www.gnu.org/licenses/gpl.txt
6
 
7
  Send comments to: jose.tejada at ieee.org
8
 
9
*/
10
 
11
#include <string>
12
#include <vector>
13
#include <algorithm>
14
#include <cstdlib>
15
#include <iostream>
16
 
17
typedef std::vector<struct argument_t*> arg_vector_t;
18
 
19
struct argument_t {
20
  std::string short_name, long_name;
21
  typedef enum { integer, text, flag } arg_type;
22
  arg_type type;
23
  std::string description;
24
  // possible states
25
  int integer_value;
26
  std::string string_value;
27
  bool state, req;
28
 
29
  argument_t( arg_vector_t& av, const char* long_name, arg_type type,
30
    const char* desc, bool required=false )
31
    : long_name(long_name), type( type ), description( desc ),
32
      req(required), state(false), integer_value(0)
33
  {
34
    if( !this->long_name.empty() ) {
35
      this->short_name = "-" + this->long_name.substr(0,1);
36
      this->long_name = "--" + this->long_name;
37
    }
38
    av.push_back(this);
39
  }
40
  void set() { state=true; }
41
  bool is_set() { return state; }
42
};
43
 
44
class Args {
45
  arg_vector_t& legal_args;
46
  argument_t* def_arg;
47
  std::string program_name;
48
  argument_t help_arg;
49
public:
50
  void throw_error( std::string x ) /*throw const char**/ { throw x.c_str(); }
51
  Args( int argc, char *argv[], arg_vector_t& legal_args ) //throw const char *
52
  : legal_args( legal_args ),
53
    help_arg( legal_args, "help", argument_t::flag, "Display usage information")
54
  {
55
    //std::copy( legal_args.begin(), legal_args.cnd(), legal_args.begin() );    
56
    if( argc== 1 ) help_arg.set(); // show help if there are no args.
57
    // look for default argument
58
    def_arg=NULL;
59
    for( int j=0; j<legal_args.size(); j++ ) {
60
      if ( legal_args[j]->short_name.empty() && legal_args[j]->long_name.empty() )
61
        if( def_arg==NULL ) def_arg = legal_args[j];
62
        else throw "Cannot set more than one default argument.";
63
    }
64
    if( def_arg && def_arg->type!=argument_t::integer && def_arg->type!=argument_t::text )
65
      throw "Default arguments can only be of type integer or text";
66
 
67
    program_name = argv[0];
68
    for( int k=1; k<argc; k++ ) {
69
      bool matched=false;
70
      for( int j=0; j<legal_args.size(); j++ ) {
71
        argument_t& a = *legal_args[j];
72
        if( a.long_name==argv[k] || a.short_name==argv[k] ) {
73
          if( a.type == argument_t::flag ) { a.set(); matched=true; continue; }
74
          if( a.type == argument_t::text ) {
75
            k++;
76
            if( k>=argc ) throw_error("Expecting input after "+a.long_name+" param");
77
            a.string_value = argv[k];
78
            a.set();
79
            matched=true;
80
            continue;
81
          }
82
          if( a.type == argument_t::integer ) {
83
            k++;
84
            if( k>=argc ) throw_error("Expecting input after "+a.long_name+" param");
85
            a.integer_value = atoi(argv[k]);
86
            a.set();
87
            matched=true;
88
            continue;
89
          }
90
        }
91
      }
92
      if( !matched && def_arg!=NULL )
93
        if( def_arg->state )
94
          throw_error( "Unknown parameter " + std::string(argv[k] ));
95
        else {
96
          if( def_arg->type==argument_t::integer ) def_arg->integer_value=atoi(argv[k]);
97
          if( def_arg->type==argument_t::text ) def_arg->string_value=argv[k];
98
          def_arg->set();
99
        }
100
    }
101
    if( help_arg.is_set() ) return; // do not perform more checks
102
    // check that all required parameters are present
103
    for( int j=0; j<legal_args.size(); j++ ) {
104
      argument_t& a = *legal_args[j];
105
      if( a.req && !a.state ) {
106
        std::string pname;
107
        if( !a.long_name.empty() ) pname=a.long_name;
108
        else if( !a.short_name.empty() ) pname=a.short_name;
109
        throw_error("Parameter "+pname+" is required.");
110
      }
111
    }
112
  }
113
  bool help_request() { if( help_arg.state ) show_help(); return help_arg.state; }
114
  std::string brackets( const argument_t& a, std::string s ) {
115
    return a.req ?  "<"+s+">" : "["+s+"]";
116
  }
117
  void show_help() {
118
    std::cout << "Usage: " << program_name << " ";
119
    if( def_arg!=NULL )
120
      std::cout << brackets( *def_arg, def_arg->description.empty() ? "parameter " : def_arg->description);
121
    std::cout << "\n";
122
    for( int j=0; j<legal_args.size(); j++ ) {
123
      argument_t& a = *legal_args[j];
124
      if( a.long_name.empty() && a.long_name.empty() ) continue;
125
      std::cout << "\t";
126
      std::string aux;
127
      if( !a.long_name.empty() ) aux = a.long_name;
128
      if( !a.long_name.empty() && !a.long_name.empty() ) aux += " | ";
129
      if( !a.short_name.empty() ) aux+= a.short_name;
130
      std::cout << brackets( a, aux );
131
      if( !a.description.empty() ) std::cout << ":  " << a.description;
132
      std::cout << "\n";
133
    }
134
  }
135
};

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.