Changeset 15d21e for src


Ignore:
Timestamp:
Jun 20, 2017, 8:02:18 PM (8 years ago)
Author:
Frederik Heber <frederik.heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_SelectAtomByNameAction, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_StructOpt_integration_tests, AutomationFragmentation_failures, Candidate_v1.6.1, ChangeBugEmailaddress, ChemicalSpaceEvaluator, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph_documentation, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, ForceAnnealing_oldresults, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, Gui_displays_atomic_force_velocity, IndependentFragmentGrids_IntegrationTest, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, StoppableMakroAction, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps
Children:
137845
Parents:
da7ef9
git-author:
Frederik Heber <heber@…> (03/23/17 17:38:46)
git-committer:
Frederik Heber <frederik.heber@…> (06/20/17 20:02:18)
Message:

CommandLineParser is now able to parse negative numbers.

  • before they were recognized as shortforms of arguments. Now, we found a nice solution on stackoverflow that preparses all options and checks whether the shortform is actually convertible to a number and skips it then.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/UIElements/CommandLineUI/CommandLineParser.cpp

    rda7ef9 r15d21e  
    3636
    3737#include <boost/filesystem.hpp>
     38#include <boost/lexical_cast.hpp>
     39#include <boost/program_options/option.hpp>
     40#include <boost/program_options/value_semantic.hpp>
    3841#include <boost/program_options.hpp>
    3942#include <fstream>
     
    461464}
    462465
     466/** This is due to the answer by Aleksey Vitebskiy
     467 * in http://stackoverflow.com/questions/4107087/accepting-negative-doubles-with-boostprogram-options
     468 *
     469 */
     470std::vector<po::option> ignore_numbers(std::vector<std::string>& args)
     471{
     472    std::vector<po::option> result;
     473    int pos = 0;
     474    while(!args.empty()) {
     475        const std::string& arg = args[0];
     476        bool isNumber = true;
     477        try {
     478          boost::lexical_cast<double>(arg);
     479        } catch(boost::bad_lexical_cast) {
     480          isNumber = false;
     481        }
     482        if (isNumber) {
     483            result.push_back(po::option());
     484            po::option& opt = result.back();
     485
     486            opt.position_key = pos++;
     487            opt.value.push_back(arg);
     488            opt.original_tokens.push_back(arg);
     489
     490            args.erase(args.begin());
     491        } else {
     492            break;
     493        }
     494    }
     495
     496    return result;
     497}
     498
    463499/** Parses the command line arguments.
    464500 * Calls program_options::store() and program_options::notify()
     
    471507  bool status = true;
    472508  try {
    473     po::store(po::command_line_parser(argc,argv).options(cmdline_options).run(), vm);
     509    po::store(po::command_line_parser(argc,argv).extra_style_parser(&ignore_numbers).options(cmdline_options).run(), vm);
    474510  } catch (std::exception &e) {
    475511    std::cerr << "Something went wrong with parsing the command-line arguments: "
Note: See TracChangeset for help on using the changeset viewer.