/* Copyright (c) 2009, Hideyuki Tanaka All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #ifdef max #undef max #endif namespace cmdline{ namespace detail{ template class lexical_cast_t{ public: static Target cast(const Source &arg){ Target ret; std::stringstream ss; if (!(ss<>ret && ss.eof())) throw std::bad_cast(); return ret; } }; template class lexical_cast_t{ public: static Target cast(const Source &arg){ return arg; } }; template class lexical_cast_t{ public: static std::string cast(const Source &arg){ std::ostringstream ss; ss< class lexical_cast_t{ public: static Target cast(const std::string &arg){ Target ret; std::istringstream ss(arg); if (!(ss>>ret && ss.eof())) throw std::bad_cast(); return ret; } }; template struct is_same { static const bool value = false; }; template struct is_same{ static const bool value = true; }; template Target lexical_cast(const Source &arg) { return lexical_cast_t::value>::cast(arg); } template std::string readable_typename(); template std::string default_value(T def) { return detail::lexical_cast(def); } template <> inline std::string readable_typename() { return "string"; } template <> inline std::string readable_typename() { return "int"; } template <> inline std::string readable_typename() { return "uint"; } } // detail //----- class cmdline_error : public std::exception { public: cmdline_error(const std::string &msg): msg(msg){} ~cmdline_error() throw() {} const char *what() const throw() { return msg.c_str(); } private: std::string msg; }; template struct default_reader{ T operator()(const std::string &str){ return detail::lexical_cast(str); } std::string description() const { return ""; } }; template struct range_reader{ range_reader(const T &low, const T &high): low(low), high(high) {} T operator()(const std::string &s) const { T ret=default_reader()(s); if (!(ret>=low && ret<=high)) throw cmdline::cmdline_error(description()); return ret; } std::string description() const { return "Must be within [" + detail::lexical_cast(low) + ", " + detail::lexical_cast(high) + "]"; } private: T low, high; }; template range_reader range(const T &low, const T &high) { return range_reader(low, high); } template struct oneof_reader{ T operator()(const std::string &s){ T ret=default_reader()(s); if (std::find(alt.begin(), alt.end(), ret)==alt.end()) throw cmdline::cmdline_error("'" + s + "' is not one of the accepted values"); return ret; } void add(const T &v){ alt.push_back(v); } std::string description() const { std::string ret = "Options are:"; for(size_t i=0; i < alt.size(); i++) ret += "\n * " + detail::lexical_cast(alt[i]); return ret; } private: std::vector alt; }; template oneof_reader oneof(T a1) { oneof_reader ret; ret.add(a1); return ret; } template oneof_reader oneof(T a1, T a2) { oneof_reader ret; ret.add(a1); ret.add(a2); return ret; } template oneof_reader oneof(T a1, T a2, T a3) { oneof_reader ret; ret.add(a1); ret.add(a2); ret.add(a3); return ret; } template oneof_reader oneof(T a1, T a2, T a3, T a4) { oneof_reader ret; ret.add(a1); ret.add(a2); ret.add(a3); ret.add(a4); return ret; } template oneof_reader oneof(T a1, T a2, T a3, T a4, T a5) { oneof_reader ret; ret.add(a1); ret.add(a2); ret.add(a3); ret.add(a4); ret.add(a5); return ret; } template oneof_reader oneof(T a1, T a2, T a3, T a4, T a5, T a6) { oneof_reader ret; ret.add(a1); ret.add(a2); ret.add(a3); ret.add(a4); ret.add(a5); ret.add(a6); return ret; } template oneof_reader oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7) { oneof_reader ret; ret.add(a1); ret.add(a2); ret.add(a3); ret.add(a4); ret.add(a5); ret.add(a6); ret.add(a7); return ret; } template oneof_reader oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7, T a8) { oneof_reader ret; ret.add(a1); ret.add(a2); ret.add(a3); ret.add(a4); ret.add(a5); ret.add(a6); ret.add(a7); ret.add(a8); return ret; } template oneof_reader oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7, T a8, T a9) { oneof_reader ret; ret.add(a1); ret.add(a2); ret.add(a3); ret.add(a4); ret.add(a5); ret.add(a6); ret.add(a7); ret.add(a8); ret.add(a9); return ret; } template oneof_reader oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7, T a8, T a9, T a10) { oneof_reader ret; ret.add(a1); ret.add(a2); ret.add(a3); ret.add(a4); ret.add(a5); ret.add(a6); ret.add(a7); ret.add(a8); ret.add(a9); ret.add(a10); return ret; } //----- class parser{ public: parser(){ stop = false; } ~parser(){ for (std::map::iterator p=options.begin(); p!=options.end(); p++) delete p->second; } void add(const std::string &name, char short_name=0, const std::string &desc=""){ if (options.count(name)) throw cmdline_error("multiple definition: "+name); options[name]=new option_without_value(name, short_name, desc); ordered.push_back(options[name]); } template void add(const std::string &name, char short_name=0, const std::string &desc="", bool need=true, const T def=T()){ add(name, short_name, desc, need, def, default_reader()); } template void add(const std::string &name, char short_name=0, const std::string &desc="", bool need=true, const T def=T(), F reader=F()){ if (options.count(name)) throw cmdline_error("multiple definition: "+name); options[name]=new option_with_value_with_reader(name, short_name, need, def, desc, reader); ordered.push_back(options[name]); } void set_header(const std::string &f){ hdr=f; } void set_footer(const std::string &f){ ftr=f; } void stop_at_rest(bool s){ stop=s; } void set_program_name(const std::string &name){ prog_name=name; } bool exist(const std::string &name) const { if (options.count(name)==0) throw cmdline_error("there is no flag: --"+name); return options.find(name)->second->has_set(); } template const T &get(const std::string &name) const { if (options.count(name)==0) throw cmdline_error("there is no flag: --"+name); const option_with_value *p=dynamic_cast*>(options.find(name)->second); if (p==NULL) throw cmdline_error("type mismatch flag '"+name+"'"); return p->get(); } const std::vector &rest() const { return others; } void set_rest(const std::vector &rest) { others = rest; } bool parse(const std::vector &args, bool processed_arg0 = false){ int argc=static_cast(args.size()); std::vector argv(argc); for (int i=0; i lookup; for (std::map::iterator p=options.begin(); p!=options.end(); p++){ if (p->first.length()==0) continue; char initial=p->second->short_name(); if (initial){ if (lookup.count(initial)>0){ lookup[initial]=""; errors.push_back(std::string("short option '")+initial+"' is ambiguous"); return false; } else lookup[initial]=p->first; } } bool found_others = false; for (int i=processed_arg0 ? 0 : 1; imust()) oss<short_description()<<" "; } oss<<"[options ...] "<name().length()); } for (size_t i=0; ishort_name()){ oss<<" -"<short_name()<<", "; } else{ oss<<" "; } oss<<"--"<name(); for (size_t j=ordered[i]->name().length(); jdescription(); // allow multiline descriptions, align them properly size_t nl = desc.find('\n'); while(nl != std::string::npos) { std::string firstline = desc.substr(0, nl); desc = desc.substr(nl+1); // print the first line oss<set()){ errors.push_back("option needs value: --"+name); return; } } void set_option(const std::string &name, const std::string &value){ if (options.count(name)==0){ errors.push_back("undefined option: --"+name); return; } if (!options[name]->set(value)){ std::string err_details = options[name]->error_details(); if(err_details.empty()) errors.push_back("option value is invalid: --"+name+"="+value); else errors.push_back("option value is invalid: --"+name+"="+value+" ("+err_details+")"); return; } } class option_base{ public: virtual ~option_base(){} virtual bool has_value() const=0; virtual bool set()=0; virtual bool set(const std::string &value)=0; virtual bool has_set() const=0; virtual bool valid() const=0; virtual bool must() const=0; virtual const std::string error_details() { return ""; } virtual const std::string &name() const=0; virtual char short_name() const=0; virtual const std::string &description() const=0; virtual std::string short_description() const=0; }; class option_without_value : public option_base { public: option_without_value(const std::string &name, char short_name, const std::string &desc) :nam(name), snam(short_name), desc(desc), has(false){ } ~option_without_value(){} bool has_value() const { return false; } bool set(){ has=true; return true; } bool set(const std::string &){ return false; } bool has_set() const { return has; } bool valid() const{ return true; } bool must() const{ return false; } const std::string &name() const{ return nam; } char short_name() const{ return snam; } const std::string &description() const { return desc; } std::string short_description() const{ return "--"+nam; } virtual const std::string error_details() { return nam+" can't have parameter"; } private: std::string nam; char snam; std::string desc; bool has; }; template class option_with_value : public option_base { public: option_with_value(const std::string &name, char short_name, bool need, const T &def, const std::string &desc) : nam(name), snam(short_name), need(need), has(false) , def(def), actual(def) { this->desc=full_description(desc); this->error = " (Unknown error)"; } ~option_with_value(){} const T &get() const { return actual; } bool has_value() const { return true; } bool set(){ return false; } bool set(const std::string &value){ try{ actual=read(value); has=true; } catch(const std::exception &e){ error = e.what(); return false; } return true; } virtual const std::string error_details() { return error; } bool has_set() const{ return has; } bool valid() const{ if (need && !has) return false; return true; } bool must() const{ return need; } const std::string &name() const{ return nam; } char short_name() const{ return snam; } const std::string &description() const { return desc; } std::string short_description() const{ return "--"+nam+"=<"+detail::readable_typename() + ">"; } protected: std::string full_description(const std::string &desc){ std::string defval = detail::default_value(def); return desc+" ("+ (need?"":"optional ")+ detail::readable_typename()+ (!need && !defval.empty() ? "="+defval : "")+ ")"; } virtual T read(const std::string &s)=0; std::string nam; char snam; bool need; std::string desc; std::string error; bool has; T def; T actual; }; template class option_with_value_with_reader : public option_with_value { public: option_with_value_with_reader(const std::string &name, char short_name, bool need, const T def, const std::string &desc, F reader) : option_with_value(name, short_name, need, def, desc), reader(reader){ std::string reader_description = this->reader.description(); if(!reader_description.empty()) this->desc = this->desc + " " + reader_description; } private: T read(const std::string &s){ return reader(s); } F reader; }; std::map options; std::vector ordered; std::string hdr; std::string ftr; bool stop; std::string prog_name; std::vector others; std::vector errors; }; } // cmdline