Ejemplo n.º 1
0
Archivo: debug.c Proyecto: fmccabe/cafe
static DebugWaitFor dbgShowArg(char *line, processPo p, termPo loc, insWord ins, void *cl) {
  integer argNo = cmdCount(line, 0);
  methodPo mtd = p->prog;
  framePo fp = p->fp;
  ptrPo sp = p->sp;

  if (argNo == 0)
    showAllArgs(debugOutChnnl, p, mtd, fp, sp);
  else if (argNo > 0 && argNo < argCount(mtd))
    showArg(debugOutChnnl, argNo, mtd, fp, sp);
  else
    outMsg(debugOutChnnl, "invalid argument number: %d", argNo);

  resetDeflt("n");
  return moreDebug;
}
Ejemplo n.º 2
0
/* getopt
 * int getopt(int argc, char *const argv[], const char *optstring);
 *
 * extern char *optarg;
 * extern int optind, opterr, optopt;
 */
int get_short_opt_arg(int argc, char **argv)
{
    // first char is +, it will stop at first nonoption arg
    // first char is -, it will return nonoption with oc == 1
    // after +/- or nothing, next char is colon, then we can distinguish
    //      between invalid option and missing option arguments.
    // The options argument is a string that specifies the option characters
    // that are valid for this program.
    //  * An option character in this string can be followed by a colon (‘:’)
    //      to indicate that it takes a required argument.
    //  * If an option character is followed by two colons (‘::’), its argument
    //      is optional;
    // This is a GNU extension.
    // Ref: https://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html
    const char *short_options = ":ab:c::h";
    int oc;
    // opterr = 1; // default, getopt print error message for invalid options
    while (1) {
        oc = getopt(argc, argv, short_options);
        if (oc == -1) { // END of getopt
            break;
        }
        switch (oc) {
            case 'a':
            case 'h':
                printf("setting [%c] flag\n", oc);
                break;
            case 'b':
                printf("setting [%c] optarg=[%s]\n", oc, optarg);
                break;
            case 'c':
                // optional opt arg
                // -c   valid, no optarg
                // -c3  valid, "3" is optarg
                // -c 3 invalid, optarg == NULL
                if (optarg == NULL) {
                    printf("setting [%c] with NO optarg\n",
                           oc);
                } else {
                    printf("setting [%c] with optarg=[%s]\n",
                           oc, optarg);
                }
                break;
            case 1:
                printf("when optstring[0]=%c, optarg=[%s]\n",
                       short_options[0], optarg);
                break;
            case ':':
                diemsg("%s: option '-%c' %x requries an arguemnts\n",
                       argv[0], optopt, optopt);
                break;
            case '?':
            default:
                diemsg("%s: option '-%c' %c is invalid: ignore\n",
                       argv[0], oc, optopt);
                break;
        }
    }
    argc -= optind;
    argv += optind;
    showArg(argc, argv);
    return 0;
}
Ejemplo n.º 3
0
int main(int argc, char** argv)
{
  try
  {
    TCLAP::CmdLine cmd("Query tool", ' ');
    TCLAP::SwitchArg showArg("", "show", "Show fields and exit", false);
    TCLAP::ValueArg<std::string> selectArg("s", "select", "Comma separated list of field names to select, if omitted, all fields are selected", false, "", "Field selection");
    TCLAP::ValueArg<std::string> filterArg("f", "filter", "Filter expression in the form FIELDNAME=\"value\", filters selction", false, "", "Filter expression");
    TCLAP::ValueArg<std::string> orderArg("o", "order", "Comma separated list of field names with which to order a selection", false, "", "Order by");
    TCLAP::ValueArg<std::string> datastoreFileArg("d", "db", "JSON database file name to load or create", false, "db.json", "Database file");
    cmd.add(showArg);
    cmd.add(selectArg);
    cmd.add(filterArg);
    cmd.add(orderArg);
    cmd.add(datastoreFileArg);
    cmd.parse(argc, argv);

    DataStore::DatabasePtrH database =
      DataStore::DataStorageJson::Load(datastoreFileArg.getValue().c_str());

    DataStore::IFieldDescriptorConstListConstPtrH allFields =
      database->getScheme()->getFieldDescriptors();

    //
    // Show fields and exit?
    //
    if (showArg.isSet())
    {
      printFields(*(allFields.get()));
      return 0;
    }

    //
    // Parse selection (-s) into a list of field descriptors
    //

    DataStore::IFieldDescriptorConstListPtrH selectedFields;
    if (selectArg.isSet())
    {
      selectedFields = parseFieldNameList(selectArg.getValue(),
        *(allFields.get()));
    }

    //
    // Parse filter (-f) expression into a logical expression AST
    //

    DataStore::Predicate filter = DataStore::Predicate::AlwaysTrue();
    if (filterArg.isSet())
    {
      DataStore::IQualifierPtrH filterAst =
        parseFilterExpression(filterArg.getValue(), *(allFields.get()));
      filter = DataStore::Predicate(filterAst);
    }

    //
    // Parse order (-o) in to list of field descriptors, order is
    // always ascending.
    //

    DataStore::IFieldDescriptorConstListPtrH orderByFields;
    if (orderArg.isSet())
    {
      orderByFields = parseFieldNameList(orderArg.getValue(),
        *(allFields.get()));
    }

    //
    // Perform query, and print result
    //

    DataStore::IQueryResultConstPtrH result =
      database->query(selectedFields, &filter, orderByFields);
    printResult(*(result.get()));
  }
  catch (TCLAP::ArgException &e)
  {
    std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
    return 1;
  }
  catch (std::exception& ex)
  {
    std::cerr << "error: " << ex.what() << std::endl;
    return 1;
  }

	return 0;
}
Ejemplo n.º 4
0
/* getopt_long
    struct option {
        const char *name; // name of option, without any dash
        int has_arg; // enum, no_argument, required_argument, optional_argument
        int *flag;
        // if flag == NULL, then getopt_long return VAL.
        // if flag != NULL, *flag = val, getopt_long return 0
        int val;
    };
    array of options, should end with all 0 field. so it could find end of
        options.
 */
int get_long_opt_arg(int argc, char **argv)
{
    int isHelp = 0;
    struct option option_list[] = {
        {"all", no_argument, NULL, 'a'},
        {"bang", required_argument, NULL, 'b'},
        {"cee", optional_argument, NULL, 'c'},
        {"dii", required_argument, NULL, 1},
        {"help", no_argument, &isHelp, 1},
        {0, 0, 0, 0}
    };
    char optstring[] = ":ab:c::h";
    int oc;
    while (1) {
        int option_index;
        oc = getopt_long(argc, argv, optstring, option_list, &option_index);
        if (oc == -1) {
            break;
        }
        switch (oc) {
            case 'b':
                printf("setting [%c] optarg=[%s]\n", oc, optarg);
                break;
            case 'a':
                printf("setting [%c] flag\n", oc);
                break;
            case 'h':
                printf("setting [%c] flag\n", oc);
                isHelp = 1;
                break;
            case 'c':
                if (optarg == NULL) {
                    printf("setting [%c] with NO optarg\n",
                           oc);
                } else {
                    printf("setting [%c] with optarg=[%s]\n",
                           oc, optarg);
                }
                break;
            case 1:
                printf("find %dth option %s, optarg=[%s]\n",
                       option_index, option_list[option_index].name, optarg);
                break;
            case 0:
                printf("auto setting *flag <= val\n");
                break;
            case ':':
                diemsg("%s: option '-%c' %x requries an arguemnts\n",
                       argv[0], optopt, optopt);
                break;
            case '?':
            default:
                diemsg("%s: option '-%c' %c is invalid: ignore\n",
                       argv[0], oc, optopt);
                break;
        }
    }
    argc -= optind;
    argv += optind;
    showArg(argc, argv);
    return 0;
}