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

Subversion Repositories sqmusic

[/] [sqmusic/] [trunk/] [cpp/] [args.h] - Diff between revs 16 and 17

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 16 Rev 17
Line 16... Line 16...
 
 
typedef std::vector<struct argument_t*> arg_vector_t;
typedef std::vector<struct argument_t*> arg_vector_t;
 
 
struct argument_t {
struct argument_t {
  std::string short_name, long_name;
  std::string short_name, long_name;
  typedef enum { integer, text, flag } arg_type;
  typedef enum { integer, text, flag, real } arg_type;
  arg_type type;
  arg_type type;
  std::string description;
  std::string description;
  // possible states
  // possible states
  int integer_value;
  int integer_value;
  std::string string_value;
  std::string string_value;
 
  float real_value;
  bool state, req;
  bool state, req;
 
 
  argument_t( arg_vector_t& av, const char* long_name, arg_type type,
  argument_t( arg_vector_t& av, const char* long_name, arg_type type,
    const char* desc, bool required=false )
    const char* desc="", bool required=false )
    : long_name(long_name), type( type ), description( desc ),
    : long_name(long_name), type( type ), description( desc ),
      req(required), state(false), integer_value(0)
      req(required), state(false), integer_value(0), real_value(0)
  {
  {
    if( !this->long_name.empty() ) {
    if( !this->long_name.empty() ) {
      this->short_name = "-" + this->long_name.substr(0,1);
      this->short_name = "-" + this->long_name.substr(0,1);
      this->long_name = "--" + this->long_name;
      this->long_name = "--" + this->long_name;
    }
    }
Line 50... Line 51...
  void throw_error( std::string x ) /*throw const char**/ { throw x.c_str(); }
  void throw_error( std::string x ) /*throw const char**/ { throw x.c_str(); }
  Args( int argc, char *argv[], arg_vector_t& legal_args ) //throw const char *
  Args( int argc, char *argv[], arg_vector_t& legal_args ) //throw const char *
  : legal_args( legal_args ),
  : legal_args( legal_args ),
    help_arg( legal_args, "help", argument_t::flag, "Display usage information")
    help_arg( legal_args, "help", argument_t::flag, "Display usage information")
  {
  {
    //std::copy( legal_args.begin(), legal_args.cnd(), legal_args.begin() );    
 
    if( argc== 1 ) help_arg.set(); // show help if there are no args.
 
    // look for default argument
    // look for default argument
    def_arg=NULL;
    def_arg=NULL;
    for( int j=0; j<legal_args.size(); j++ ) {
    for( int j=0; j<legal_args.size(); j++ ) {
      if ( legal_args[j]->short_name.empty() && legal_args[j]->long_name.empty() )
      if ( legal_args[j]->short_name.empty() && legal_args[j]->long_name.empty() )
        if( def_arg==NULL ) def_arg = legal_args[j];
        if( def_arg==NULL ) def_arg = legal_args[j];
Line 85... Line 84...
            a.integer_value = atoi(argv[k]);
            a.integer_value = atoi(argv[k]);
            a.set();
            a.set();
            matched=true;
            matched=true;
            continue;
            continue;
          }
          }
 
          if( a.type == argument_t::real ) {
 
            k++;
 
            if( k>=argc ) throw_error("Expecting input after "+a.long_name+" param");
 
            a.real_value = atof(argv[k]);
 
            a.set();
 
            matched=true;
 
            continue;
 
          }
        }
        }
      }
      }
      if( !matched && def_arg!=NULL )
      if( !matched && def_arg!=NULL )
        if( def_arg->state )
        if( def_arg->state )
          throw_error( "Unknown parameter " + std::string(argv[k] ));
          throw_error( "Unknown parameter " + std::string(argv[k] ));
Line 108... Line 115...
        else if( !a.short_name.empty() ) pname=a.short_name;
        else if( !a.short_name.empty() ) pname=a.short_name;
        throw_error("Parameter "+pname+" is required.");
        throw_error("Parameter "+pname+" is required.");
      }
      }
    }
    }
  }
  }
  bool help_request() { if( help_arg.state ) show_help(); return help_arg.state; }
  bool help_request() { if( help_arg.is_set() ) show_help(); return help_arg.is_set(); }
  std::string brackets( const argument_t& a, std::string s ) {
  std::string brackets( const argument_t& a, std::string s ) {
    return a.req ?  "<"+s+">" : "["+s+"]";
    return a.req ?  "<"+s+">" : "["+s+"]";
  }
  }
  void show_help() {
  void show_help() {
    std::cout << "Usage: " << program_name << " ";
    std::cout << "Usage: " << program_name << " ";
Line 126... Line 133...
      std::string aux;
      std::string aux;
      if( !a.long_name.empty() ) aux = a.long_name;
      if( !a.long_name.empty() ) aux = a.long_name;
      if( !a.long_name.empty() && !a.long_name.empty() ) aux += " | ";
      if( !a.long_name.empty() && !a.long_name.empty() ) aux += " | ";
      if( !a.short_name.empty() ) aux+= a.short_name;
      if( !a.short_name.empty() ) aux+= a.short_name;
      std::cout << brackets( a, aux );
      std::cout << brackets( a, aux );
 
      switch( a.type ) {
 
        case argument_t::integer: std::cout << " followed by integer number. "; break;
 
        case argument_t::real   : std::cout << " followed by real number. "; break;
 
        case argument_t::text   : std::cout << " followed by string. "; break;
 
      }
      if( !a.description.empty() ) std::cout << ":  " << a.description;
      if( !a.description.empty() ) std::cout << ":  " << a.description;
      std::cout << "\n";
      std::cout << "\n";
    }
    }
  }
  }
};
};

powered by: WebSVN 2.1.0

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