예제 #1
0
	void ArgParse::ParseCommand(char* args[]) const
	{
		set<string> requireOptions(this->requireOptions);
		map<string, pair<OptionHandler, int>>::const_iterator iter;
		if (args[0] == nullptr)
		{
			throw ArgumentDeficientException("PROGRAM PATH");
		}
		char* program = *(args++);
		
		for ( ;*args != nullptr; args ++)
		{
			char* p = *args;
			if (*p == '-' && (iter = option2Handler.find(p)) != option2Handler.end())
			{
				const int arg_num = iter->second.second;
				// retrieve next arg_num arguments
				vector<char*> opt_args(arg_num);
				for (int i = 0; i < arg_num && *(++args); i++)
					opt_args[i] = *args;
				if (*args == nullptr)
					throw ArgumentDeficientException(p);

				(iter->second.first)(opt_args);
				requireOptions.erase(p);
			}
			else if (no_option_handler != nullptr)
				no_option_handler(*args);
			else
				throw TooMoreArgumentException();
		}
		if (requireOptions.size() > 0)
			throw OptionDeficientException(*(requireOptions.begin()));
	}
예제 #2
0
파일: main.c 프로젝트: AdityaSarang/pdsh
int main(int argc, char *argv[])
{
    opt_t opt;
    int retval = 0;
    const char *m;


    /*
     * Initialize.
     */
    err_init(xbasename(argv[0]));       /* init err package */

    /*
     *  If running setuid, fork a child to handle 
     *   all privileged operations and drop privs in this process.
     */
    privsep_init();

    /*
     * Seed options with default values:
     */
    opt_default(&opt, argv[0]);

    /*
     * Override defaults with environment
     */
    opt_env(&opt);

    /*
     * Process any options that need to be handled early:
     */
    opt_args_early(&opt, argc, argv);

    /*
     *  Load static or dynamic pdsh modules
     */
    mod_init();
    /*
     *  Allow module directory to be overridden, but not when
     *   running as root or setuid. (This is mainly for testing...)
     */
    if (!(m = getenv ("PDSH_MODULE_DIR")) ||
          getuid() == 0 ||
          getuid() != geteuid())
        m = pdsh_module_dir;
    if (mod_load_modules(m, &opt) < 0)
        errx("%p: Couldn't load any pdsh modules\n");

    /*
     * Handle options.
     */
    opt_args(&opt, argc, argv); /* override with command line           */

    if (opt_verify(&opt)) {     /* verify options, print errors         */
        /*
         * Do the work.
         */
        if (opt.info_only)      /* display info only */
            opt_list(&opt);
        else if (pdsh_personality() == PCP && opt.pcp_server) 
            retval = (_pcp_remote_server (&opt) < 0);
        else if (pdsh_personality() == PCP && opt.pcp_client)
            retval = (_pcp_remote_client (&opt) < 0);
        else if (pdsh_personality() == PCP || opt.cmd != NULL)
            retval = dsh(&opt); /* single dsh/pcp command */
        else                    /* prompt loop */
            _interactive_dsh(&opt);
    } else {
        retval = 1;
    }

    mod_exit(); 

    /*
     * Clean up.
     */
    privsep_fini();
    opt_free(&opt);             /* free heap storage in opt struct */
    err_cleanup();

    return retval;
}
예제 #3
0
int main(int argc, char ** argv)
{
    std::string path = ".";
    bool list_all = false;
    bool long_listing = false;
    sort_type_t sort_type = DEFAULT_SORT;


    namespace po = boost::program_options;
    po::options_description opt_args("Optional arguments");
    opt_args.add_options()
        ("long,l", "print long listings for files")
        ("all,a", "list all files (including dot files)")
        ("reverse,r", "sort listings in reverse order")
        ("time,t", "sort by last modification time")
        ("help,h", "print this message and exit")
    ;
    po::options_description hidden_args;
    hidden_args.add_options()
        ("path", po::value<std::string>(), "");
    po::positional_options_description po_desc;
    po_desc.add("path", 1);

    try
    {
        po::variables_map vm;
        po::options_description all_args;
        all_args.add(opt_args).add(hidden_args);
        po::store(po::command_line_parser(argc,argv).
                  options(all_args).positional(po_desc).run(), vm);
        po::notify(vm);

        if (vm.count("help"))
        {
            std::cout << "USAGE: list_test [path]\n";
            std::cout << opt_args << std::endl;
            std::exit(1);
        }
        if (vm.count("path"))
        {
            path = vm["path"].as<std::string>();
        }
        if (vm.count("long"))
        {
            long_listing = true;
        }
        if (vm.count("all"))
        {
            list_all = true;
        }
        if (vm.count("reverse") && vm.count("time"))
        {
            sort_type = MODTIME_REVERSE;
        }
        else if (vm.count("time"))
        {
            sort_type = MODTIME;
        }
        else if (vm.count("reverse"))
        {
            sort_type = NAME_REVERSE;
        }
    }
    catch ( const boost::program_options::error &e )
    {
        std::cerr << e.what() << std::endl;
        std::exit(1);
    }

    print_dir_listing(path.c_str(),long_listing,list_all,sort_type);
}