|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283 |
- /* A simple header-only C++ argument parser library.
- *
- * https://github.com/Taywee/args
- *
- * Copyright (c) 2016-2019 Taylor C. Richberger <taywee@gmx.com> and Pavel
- * Belikov
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to
- * deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
- */
-
- /** \file args.hxx
- * \brief this single-header lets you use all of the args functionality
- *
- * The important stuff is done inside the args namespace
- */
-
- #ifndef ARGS_HXX
- #define ARGS_HXX
-
- #include <algorithm>
- #include <iterator>
- #include <exception>
- #include <functional>
- #include <sstream>
- #include <string>
- #include <tuple>
- #include <vector>
- #include <unordered_map>
- #include <unordered_set>
- #include <type_traits>
- #include <cstddef>
-
- #ifdef ARGS_TESTNAMESPACE
- namespace argstest
- {
- #else
-
- /** \namespace args
- * \brief contains all the functionality of the args library
- */
- namespace args
- {
- #endif
- /** Getter to grab the value from the argument type.
- *
- * If the Get() function of the type returns a reference, so does this, and
- * the value will be modifiable.
- */
- template <typename Option>
- auto get(Option &option_) -> decltype(option_.Get())
- {
- return option_.Get();
- }
-
- /** (INTERNAL) Count UTF-8 glyphs
- *
- * This is not reliable, and will fail for combinatory glyphs, but it's
- * good enough here for now.
- *
- * \param string The string to count glyphs from
- * \return The UTF-8 glyphs in the string
- */
- inline std::string::size_type Glyphs(const std::string &string_)
- {
- std::string::size_type length = 0;
- for (const char c: string_)
- {
- if ((c & 0xc0) != 0x80)
- {
- ++length;
- }
- }
- return length;
- }
-
- /** (INTERNAL) Wrap a vector of words into a vector of lines
- *
- * Empty words are skipped. Word "\n" forces wrapping.
- *
- * \param begin The begin iterator
- * \param end The end iterator
- * \param width The width of the body
- * \param firstlinewidth the width of the first line, defaults to the width of the body
- * \param firstlineindent the indent of the first line, defaults to 0
- * \return the vector of lines
- */
- template <typename It>
- inline std::vector<std::string> Wrap(It begin,
- It end,
- const std::string::size_type width,
- std::string::size_type firstlinewidth = 0,
- std::string::size_type firstlineindent = 0)
- {
- std::vector<std::string> output;
- std::string line(firstlineindent, ' ');
- bool empty = true;
-
- if (firstlinewidth == 0)
- {
- firstlinewidth = width;
- }
-
- auto currentwidth = firstlinewidth;
-
- for (auto it = begin; it != end; ++it)
- {
- if (it->empty())
- {
- continue;
- }
-
- if (*it == "\n")
- {
- if (!empty)
- {
- output.push_back(line);
- line.clear();
- empty = true;
- currentwidth = width;
- }
-
- continue;
- }
-
- auto itemsize = Glyphs(*it);
- if ((line.length() + 1 + itemsize) > currentwidth)
- {
- if (!empty)
- {
- output.push_back(line);
- line.clear();
- empty = true;
- currentwidth = width;
- }
- }
-
- if (itemsize > 0)
- {
- if (!empty)
- {
- line += ' ';
- }
-
- line += *it;
- empty = false;
- }
- }
-
- if (!empty)
- {
- output.push_back(line);
- }
-
- return output;
- }
-
- namespace detail
- {
- template <typename T>
- std::string Join(const T& array, const std::string &delimiter)
- {
- std::string res;
- for (auto &element : array)
- {
- if (!res.empty())
- {
- res += delimiter;
- }
-
- res += element;
- }
-
- return res;
- }
- }
-
- /** (INTERNAL) Wrap a string into a vector of lines
- *
- * This is quick and hacky, but works well enough. You can specify a
- * different width for the first line
- *
- * \param width The width of the body
- * \param firstlinewid the width of the first line, defaults to the width of the body
- * \return the vector of lines
- */
- inline std::vector<std::string> Wrap(const std::string &in, const std::string::size_type width, std::string::size_type firstlinewidth = 0)
- {
- // Preserve existing line breaks
- const auto newlineloc = in.find('\n');
- if (newlineloc != in.npos)
- {
- auto first = Wrap(std::string(in, 0, newlineloc), width);
- auto second = Wrap(std::string(in, newlineloc + 1), width);
- first.insert(
- std::end(first),
- std::make_move_iterator(std::begin(second)),
- std::make_move_iterator(std::end(second)));
- return first;
- }
-
- std::istringstream stream(in);
- std::string::size_type indent = 0;
-
- for (char c : in)
- {
- if (!isspace(c))
- {
- break;
- }
- ++indent;
- }
-
- return Wrap(std::istream_iterator<std::string>(stream), std::istream_iterator<std::string>(),
- width, firstlinewidth, indent);
- }
-
- #ifdef ARGS_NOEXCEPT
- /// Error class, for when ARGS_NOEXCEPT is defined
- enum class Error
- {
- None,
- Usage,
- Parse,
- Validation,
- Required,
- Map,
- Extra,
- Help,
- Subparser,
- Completion,
- };
- #else
- /** Base error class
- */
- class Error : public std::runtime_error
- {
- public:
- Error(const std::string &problem) : std::runtime_error(problem) {}
- virtual ~Error() {}
- };
-
- /** Errors that occur during usage
- */
- class UsageError : public Error
- {
- public:
- UsageError(const std::string &problem) : Error(problem) {}
- virtual ~UsageError() {}
- };
-
- /** Errors that occur during regular parsing
- */
- class ParseError : public Error
- {
- public:
- ParseError(const std::string &problem) : Error(problem) {}
- virtual ~ParseError() {}
- };
-
- /** Errors that are detected from group validation after parsing finishes
- */
- class ValidationError : public Error
- {
- public:
- ValidationError(const std::string &problem) : Error(problem) {}
- virtual ~ValidationError() {}
- };
-
- /** Errors that when a required flag is omitted
- */
- class RequiredError : public ValidationError
- {
- public:
- RequiredError(const std::string &problem) : ValidationError(problem) {}
- virtual ~RequiredError() {}
- };
-
- /** Errors in map lookups
- */
- class MapError : public ParseError
- {
- public:
- MapError(const std::string &problem) : ParseError(problem) {}
- virtual ~MapError() {}
- };
-
- /** Error that occurs when a singular flag is specified multiple times
- */
- class ExtraError : public ParseError
- {
- public:
- ExtraError(const std::string &problem) : ParseError(problem) {}
- virtual ~ExtraError() {}
- };
-
- /** An exception that indicates that the user has requested help
- */
- class Help : public Error
- {
- public:
- Help(const std::string &flag) : Error(flag) {}
- virtual ~Help() {}
- };
-
- /** (INTERNAL) An exception that emulates coroutine-like control flow for subparsers.
- */
- class SubparserError : public Error
- {
- public:
- SubparserError() : Error("") {}
- virtual ~SubparserError() {}
- };
-
- /** An exception that contains autocompletion reply
- */
- class Completion : public Error
- {
- public:
- Completion(const std::string &flag) : Error(flag) {}
- virtual ~Completion() {}
- };
- #endif
-
- /** A simple unified option type for unified initializer lists for the Matcher class.
- */
- struct EitherFlag
- {
- const bool isShort;
- const char shortFlag;
- const std::string longFlag;
- EitherFlag(const std::string &flag) : isShort(false), shortFlag(), longFlag(flag) {}
- EitherFlag(const char *flag) : isShort(false), shortFlag(), longFlag(flag) {}
- EitherFlag(const char flag) : isShort(true), shortFlag(flag), longFlag() {}
-
- /** Get just the long flags from an initializer list of EitherFlags
- */
- static std::unordered_set<std::string> GetLong(std::initializer_list<EitherFlag> flags)
- {
- std::unordered_set<std::string> longFlags;
- for (const EitherFlag &flag: flags)
- {
- if (!flag.isShort)
- {
- longFlags.insert(flag.longFlag);
- }
- }
- return longFlags;
- }
-
- /** Get just the short flags from an initializer list of EitherFlags
- */
- static std::unordered_set<char> GetShort(std::initializer_list<EitherFlag> flags)
- {
- std::unordered_set<char> shortFlags;
- for (const EitherFlag &flag: flags)
- {
- if (flag.isShort)
- {
- shortFlags.insert(flag.shortFlag);
- }
- }
- return shortFlags;
- }
-
- std::string str() const
- {
- return isShort ? std::string(1, shortFlag) : longFlag;
- }
-
- std::string str(const std::string &shortPrefix, const std::string &longPrefix) const
- {
- return isShort ? shortPrefix + std::string(1, shortFlag) : longPrefix + longFlag;
- }
- };
-
-
-
- /** A class of "matchers", specifying short and flags that can possibly be
- * matched.
- *
- * This is supposed to be constructed and then passed in, not used directly
- * from user code.
- */
- class Matcher
- {
- private:
- const std::unordered_set<char> shortFlags;
- const std::unordered_set<std::string> longFlags;
-
- public:
- /** Specify short and long flags separately as iterators
- *
- * ex: `args::Matcher(shortFlags.begin(), shortFlags.end(), longFlags.begin(), longFlags.end())`
- */
- template <typename ShortIt, typename LongIt>
- Matcher(ShortIt shortFlagsStart, ShortIt shortFlagsEnd, LongIt longFlagsStart, LongIt longFlagsEnd) :
- shortFlags(shortFlagsStart, shortFlagsEnd),
- longFlags(longFlagsStart, longFlagsEnd)
- {
- if (shortFlags.empty() && longFlags.empty())
- {
- #ifndef ARGS_NOEXCEPT
- throw UsageError("empty Matcher");
- #endif
- }
- }
-
- #ifdef ARGS_NOEXCEPT
- /// Only for ARGS_NOEXCEPT
- Error GetError() const noexcept
- {
- return shortFlags.empty() && longFlags.empty() ? Error::Usage : Error::None;
- }
- #endif
-
- /** Specify short and long flags separately as iterables
- *
- * ex: `args::Matcher(shortFlags, longFlags)`
- */
- template <typename Short, typename Long>
- Matcher(Short &&shortIn, Long &&longIn) :
- Matcher(std::begin(shortIn), std::end(shortIn), std::begin(longIn), std::end(longIn))
- {}
-
- /** Specify a mixed single initializer-list of both short and long flags
- *
- * This is the fancy one. It takes a single initializer list of
- * any number of any mixed kinds of flags. Chars are
- * automatically interpreted as short flags, and strings are
- * automatically interpreted as long flags:
- *
- * args::Matcher{'a'}
- * args::Matcher{"foo"}
- * args::Matcher{'h', "help"}
- * args::Matcher{"foo", 'f', 'F', "FoO"}
- */
- Matcher(std::initializer_list<EitherFlag> in) :
- Matcher(EitherFlag::GetShort(in), EitherFlag::GetLong(in)) {}
-
- Matcher(Matcher &&other) : shortFlags(std::move(other.shortFlags)), longFlags(std::move(other.longFlags))
- {}
-
- ~Matcher() {}
-
- /** (INTERNAL) Check if there is a match of a short flag
- */
- bool Match(const char flag) const
- {
- return shortFlags.find(flag) != shortFlags.end();
- }
-
- /** (INTERNAL) Check if there is a match of a long flag
- */
- bool Match(const std::string &flag) const
- {
- return longFlags.find(flag) != longFlags.end();
- }
-
- /** (INTERNAL) Check if there is a match of a flag
- */
- bool Match(const EitherFlag &flag) const
- {
- return flag.isShort ? Match(flag.shortFlag) : Match(flag.longFlag);
- }
-
- /** (INTERNAL) Get all flag strings as a vector, with the prefixes embedded
- */
- std::vector<EitherFlag> GetFlagStrings() const
- {
- std::vector<EitherFlag> flagStrings;
- flagStrings.reserve(shortFlags.size() + longFlags.size());
- for (const char flag: shortFlags)
- {
- flagStrings.emplace_back(flag);
- }
- for (const std::string &flag: longFlags)
- {
- flagStrings.emplace_back(flag);
- }
- return flagStrings;
- }
-
- /** (INTERNAL) Get long flag if it exists or any short flag
- */
- EitherFlag GetLongOrAny() const
- {
- if (!longFlags.empty())
- {
- return *longFlags.begin();
- }
-
- if (!shortFlags.empty())
- {
- return *shortFlags.begin();
- }
-
- // should be unreachable
- return ' ';
- }
-
- /** (INTERNAL) Get short flag if it exists or any long flag
- */
- EitherFlag GetShortOrAny() const
- {
- if (!shortFlags.empty())
- {
- return *shortFlags.begin();
- }
-
- if (!longFlags.empty())
- {
- return *longFlags.begin();
- }
-
- // should be unreachable
- return ' ';
- }
- };
-
- /** Attributes for flags.
- */
- enum class Options
- {
- /** Default options.
- */
- None = 0x0,
-
- /** Flag can't be passed multiple times.
- */
- Single = 0x01,
-
- /** Flag can't be omitted.
- */
- Required = 0x02,
-
- /** Flag is excluded from usage line.
- */
- HiddenFromUsage = 0x04,
-
- /** Flag is excluded from options help.
- */
- HiddenFromDescription = 0x08,
-
- /** Flag is global and can be used in any subcommand.
- */
- Global = 0x10,
-
- /** Flag stops a parser.
- */
- KickOut = 0x20,
-
- /** Flag is excluded from auto completion.
- */
- HiddenFromCompletion = 0x40,
-
- /** Flag is excluded from options help and usage line
- */
- Hidden = HiddenFromUsage | HiddenFromDescription | HiddenFromCompletion,
- };
-
- inline Options operator | (Options lhs, Options rhs)
- {
- return static_cast<Options>(static_cast<int>(lhs) | static_cast<int>(rhs));
- }
-
- inline Options operator & (Options lhs, Options rhs)
- {
- return static_cast<Options>(static_cast<int>(lhs) & static_cast<int>(rhs));
- }
-
- class FlagBase;
- class PositionalBase;
- class Command;
- class ArgumentParser;
-
- /** A simple structure of parameters for easy user-modifyable help menus
- */
- struct HelpParams
- {
- /** The width of the help menu
- */
- unsigned int width = 80;
- /** The indent of the program line
- */
- unsigned int progindent = 2;
- /** The indent of the program trailing lines for long parameters
- */
- unsigned int progtailindent = 4;
- /** The indent of the description and epilogs
- */
- unsigned int descriptionindent = 4;
- /** The indent of the flags
- */
- unsigned int flagindent = 6;
- /** The indent of the flag descriptions
- */
- unsigned int helpindent = 40;
- /** The additional indent each group adds
- */
- unsigned int eachgroupindent = 2;
-
- /** The minimum gutter between each flag and its help
- */
- unsigned int gutter = 1;
-
- /** Show the terminator when both options and positional parameters are present
- */
- bool showTerminator = true;
-
- /** Show the {OPTIONS} on the prog line when this is true
- */
- bool showProglineOptions = true;
-
- /** Show the positionals on the prog line when this is true
- */
- bool showProglinePositionals = true;
-
- /** The prefix for short flags
- */
- std::string shortPrefix;
-
- /** The prefix for long flags
- */
- std::string longPrefix;
-
- /** The separator for short flags
- */
- std::string shortSeparator;
-
- /** The separator for long flags
- */
- std::string longSeparator;
-
- /** The program name for help generation
- */
- std::string programName;
-
- /** Show command's flags
- */
- bool showCommandChildren = false;
-
- /** Show command's descriptions and epilog
- */
- bool showCommandFullHelp = false;
-
- /** The postfix for progline when showProglineOptions is true and command has any flags
- */
- std::string proglineOptions = "{OPTIONS}";
-
- /** The prefix for progline when command has any subcommands
- */
- std::string proglineCommand = "COMMAND";
-
- /** The prefix for progline value
- */
- std::string proglineValueOpen = " <";
-
- /** The postfix for progline value
- */
- std::string proglineValueClose = ">";
-
- /** The prefix for progline required argument
- */
- std::string proglineRequiredOpen = "";
-
- /** The postfix for progline required argument
- */
- std::string proglineRequiredClose = "";
-
- /** The prefix for progline non-required argument
- */
- std::string proglineNonrequiredOpen = "[";
-
- /** The postfix for progline non-required argument
- */
- std::string proglineNonrequiredClose = "]";
-
- /** Show flags in program line
- */
- bool proglineShowFlags = false;
-
- /** Use short flags in program lines when possible
- */
- bool proglinePreferShortFlags = false;
-
- /** Program line prefix
- */
- std::string usageString;
-
- /** String shown in help before flags descriptions
- */
- std::string optionsString = "OPTIONS:";
-
- /** Display value name after all the long and short flags
- */
- bool useValueNameOnce = false;
-
- /** Show value name
- */
- bool showValueName = true;
-
- /** Add newline before flag description
- */
- bool addNewlineBeforeDescription = false;
-
- /** The prefix for option value
- */
- std::string valueOpen = "[";
-
- /** The postfix for option value
- */
- std::string valueClose = "]";
-
- /** Add choices to argument description
- */
- bool addChoices = false;
-
- /** The prefix for choices
- */
- std::string choiceString = "\nOne of: ";
-
- /** Add default values to argument description
- */
- bool addDefault = false;
-
- /** The prefix for default values
- */
- std::string defaultString = "\nDefault: ";
- };
-
- /** A number of arguments which can be consumed by an option.
- *
- * Represents a closed interval [min, max].
- */
- struct Nargs
- {
- const size_t min;
- const size_t max;
-
- Nargs(size_t min_, size_t max_) : min{min_}, max{max_}
- {
- #ifndef ARGS_NOEXCEPT
- if (max < min)
- {
- throw UsageError("Nargs: max > min");
- }
- #endif
- }
-
- Nargs(size_t num_) : min{num_}, max{num_}
- {
- }
-
- friend bool operator == (const Nargs &lhs, const Nargs &rhs)
- {
- return lhs.min == rhs.min && lhs.max == rhs.max;
- }
-
- friend bool operator != (const Nargs &lhs, const Nargs &rhs)
- {
- return !(lhs == rhs);
- }
- };
-
- /** Base class for all match types
- */
- class Base
- {
- private:
- Options options = {};
-
- protected:
- bool matched = false;
- const std::string help;
- #ifdef ARGS_NOEXCEPT
- /// Only for ARGS_NOEXCEPT
- mutable Error error = Error::None;
- mutable std::string errorMsg;
- #endif
-
- public:
- Base(const std::string &help_, Options options_ = {}) : options(options_), help(help_) {}
- virtual ~Base() {}
-
- Options GetOptions() const noexcept
- {
- return options;
- }
-
- bool IsRequired() const noexcept
- {
- return (GetOptions() & Options::Required) != Options::None;
- }
-
- virtual bool Matched() const noexcept
- {
- return matched;
- }
-
- virtual void Validate(const std::string &, const std::string &) const
- {
- }
-
- operator bool() const noexcept
- {
- return Matched();
- }
-
- virtual std::vector<std::tuple<std::string, std::string, unsigned>> GetDescription(const HelpParams &, const unsigned indentLevel) const
- {
- std::tuple<std::string, std::string, unsigned> description;
- std::get<1>(description) = help;
- std::get<2>(description) = indentLevel;
- return { std::move(description) };
- }
-
- virtual std::vector<Command*> GetCommands()
- {
- return {};
- }
-
- virtual bool IsGroup() const
- {
- return false;
- }
-
- virtual FlagBase *Match(const EitherFlag &)
- {
- return nullptr;
- }
-
- virtual PositionalBase *GetNextPositional()
- {
- return nullptr;
- }
-
- virtual std::vector<FlagBase*> GetAllFlags()
- {
- return {};
- }
-
- virtual bool HasFlag() const
- {
- return false;
- }
-
- virtual bool HasPositional() const
- {
- return false;
- }
-
- virtual bool HasCommand() const
- {
- return false;
- }
-
- virtual std::vector<std::string> GetProgramLine(const HelpParams &) const
- {
- return {};
- }
-
- /// Sets a kick-out value for building subparsers
- void KickOut(bool kickout_) noexcept
- {
- if (kickout_)
- {
- options = options | Options::KickOut;
- }
- else
- {
- options = static_cast<Options>(static_cast<int>(options) & ~static_cast<int>(Options::KickOut));
- }
- }
-
- /// Gets the kick-out value for building subparsers
- bool KickOut() const noexcept
- {
- return (options & Options::KickOut) != Options::None;
- }
-
- virtual void Reset() noexcept
- {
- matched = false;
- #ifdef ARGS_NOEXCEPT
- error = Error::None;
- errorMsg.clear();
- #endif
- }
-
- #ifdef ARGS_NOEXCEPT
- /// Only for ARGS_NOEXCEPT
- virtual Error GetError() const
- {
- return error;
- }
-
- /// Only for ARGS_NOEXCEPT
- std::string GetErrorMsg() const
- {
- return errorMsg;
- }
- #endif
- };
-
- /** Base class for all match types that have a name
- */
- class NamedBase : public Base
- {
- protected:
- const std::string name;
- bool kickout = false;
- std::string defaultString;
- bool defaultStringManual = false;
- std::vector<std::string> choicesStrings;
- bool choicesStringManual = false;
-
- virtual std::string GetDefaultString(const HelpParams&) const { return {}; }
-
- virtual std::vector<std::string> GetChoicesStrings(const HelpParams&) const { return {}; }
-
- virtual std::string GetNameString(const HelpParams&) const { return Name(); }
-
- void AddDescriptionPostfix(std::string &dest, const bool isManual, const std::string &manual, bool isGenerated, const std::string &generated, const std::string &str) const
- {
- if (isManual && !manual.empty())
- {
- dest += str;
- dest += manual;
- }
- else if (!isManual && isGenerated && !generated.empty())
- {
- dest += str;
- dest += generated;
- }
- }
-
- public:
- NamedBase(const std::string &name_, const std::string &help_, Options options_ = {}) : Base(help_, options_), name(name_) {}
- virtual ~NamedBase() {}
-
- /** Sets default value string that will be added to argument description.
- * Use empty string to disable it for this argument.
- */
- void HelpDefault(const std::string &str)
- {
- defaultStringManual = true;
- defaultString = str;
- }
-
- /** Gets default value string that will be added to argument description.
- */
- std::string HelpDefault(const HelpParams ¶ms) const
- {
- return defaultStringManual ? defaultString : GetDefaultString(params);
- }
-
- /** Sets choices strings that will be added to argument description.
- * Use empty vector to disable it for this argument.
- */
- void HelpChoices(const std::vector<std::string> &array)
- {
- choicesStringManual = true;
- choicesStrings = array;
- }
-
- /** Gets choices strings that will be added to argument description.
- */
- std::vector<std::string> HelpChoices(const HelpParams ¶ms) const
- {
- return choicesStringManual ? choicesStrings : GetChoicesStrings(params);
- }
-
- virtual std::vector<std::tuple<std::string, std::string, unsigned>> GetDescription(const HelpParams ¶ms, const unsigned indentLevel) const override
- {
- std::tuple<std::string, std::string, unsigned> description;
- std::get<0>(description) = GetNameString(params);
- std::get<1>(description) = help;
- std::get<2>(description) = indentLevel;
-
- AddDescriptionPostfix(std::get<1>(description), choicesStringManual, detail::Join(choicesStrings, ", "), params.addChoices, detail::Join(GetChoicesStrings(params), ", "), params.choiceString);
- AddDescriptionPostfix(std::get<1>(description), defaultStringManual, defaultString, params.addDefault, GetDefaultString(params), params.defaultString);
-
- return { std::move(description) };
- }
-
- virtual std::string Name() const
- {
- return name;
- }
- };
-
- namespace detail
- {
- template <typename T, typename = int>
- struct IsConvertableToString : std::false_type {};
-
- template <typename T>
- struct IsConvertableToString<T, decltype(std::declval<std::ostringstream&>() << std::declval<T>(), int())> : std::true_type {};
-
- template <typename T>
- typename std::enable_if<IsConvertableToString<T>::value, std::string>::type
- ToString(const T &value)
- {
- std::ostringstream s;
- s << value;
- return s.str();
- }
-
- template <typename T>
- typename std::enable_if<!IsConvertableToString<T>::value, std::string>::type
- ToString(const T &)
- {
- return {};
- }
-
- template <typename T>
- std::vector<std::string> MapKeysToStrings(const T &map)
- {
- std::vector<std::string> res;
- using K = typename std::decay<decltype(std::begin(map)->first)>::type;
- if (IsConvertableToString<K>::value)
- {
- for (const auto &p : map)
- {
- res.push_back(detail::ToString(p.first));
- }
-
- std::sort(res.begin(), res.end());
- }
- return res;
- }
- }
-
- /** Base class for all flag options
- */
- class FlagBase : public NamedBase
- {
- protected:
- const Matcher matcher;
-
- virtual std::string GetNameString(const HelpParams ¶ms) const override
- {
- const std::string postfix = !params.showValueName || NumberOfArguments() == 0 ? std::string() : Name();
- std::string flags;
- const auto flagStrings = matcher.GetFlagStrings();
- const bool useValueNameOnce = flagStrings.size() == 1 ? false : params.useValueNameOnce;
- for (auto it = flagStrings.begin(); it != flagStrings.end(); ++it)
- {
- auto &flag = *it;
- if (it != flagStrings.begin())
- {
- flags += ", ";
- }
-
- flags += flag.isShort ? params.shortPrefix : params.longPrefix;
- flags += flag.str();
-
- if (!postfix.empty() && (!useValueNameOnce || it + 1 == flagStrings.end()))
- {
- flags += flag.isShort ? params.shortSeparator : params.longSeparator;
- flags += params.valueOpen + postfix + params.valueClose;
- }
- }
-
- return flags;
- }
-
- public:
- FlagBase(const std::string &name_, const std::string &help_, Matcher &&matcher_, const bool extraError_ = false) : NamedBase(name_, help_, extraError_ ? Options::Single : Options()), matcher(std::move(matcher_)) {}
-
- FlagBase(const std::string &name_, const std::string &help_, Matcher &&matcher_, Options options_) : NamedBase(name_, help_, options_), matcher(std::move(matcher_)) {}
-
- virtual ~FlagBase() {}
-
- virtual FlagBase *Match(const EitherFlag &flag) override
- {
- if (matcher.Match(flag))
- {
- if ((GetOptions() & Options::Single) != Options::None && matched)
- {
- std::ostringstream problem;
- problem << "Flag '" << flag.str() << "' was passed multiple times, but is only allowed to be passed once";
- #ifdef ARGS_NOEXCEPT
- error = Error::Extra;
- errorMsg = problem.str();
- #else
- throw ExtraError(problem.str());
- #endif
- }
- matched = true;
- return this;
- }
- return nullptr;
- }
-
- virtual std::vector<FlagBase*> GetAllFlags() override
- {
- return { this };
- }
-
- const Matcher &GetMatcher() const
- {
- return matcher;
- }
-
- virtual void Validate(const std::string &shortPrefix, const std::string &longPrefix) const override
- {
- if (!Matched() && IsRequired())
- {
- std::ostringstream problem;
- problem << "Flag '" << matcher.GetLongOrAny().str(shortPrefix, longPrefix) << "' is required";
- #ifdef ARGS_NOEXCEPT
- error = Error::Required;
- errorMsg = problem.str();
- #else
- throw RequiredError(problem.str());
- #endif
- }
- }
-
- virtual std::vector<std::string> GetProgramLine(const HelpParams ¶ms) const override
- {
- if (!params.proglineShowFlags)
- {
- return {};
- }
-
- const std::string postfix = NumberOfArguments() == 0 ? std::string() : Name();
- const EitherFlag flag = params.proglinePreferShortFlags ? matcher.GetShortOrAny() : matcher.GetLongOrAny();
- std::string res = flag.str(params.shortPrefix, params.longPrefix);
- if (!postfix.empty())
- {
- res += params.proglineValueOpen + postfix + params.proglineValueClose;
- }
-
- return { IsRequired() ? params.proglineRequiredOpen + res + params.proglineRequiredClose
- : params.proglineNonrequiredOpen + res + params.proglineNonrequiredClose };
- }
-
- virtual bool HasFlag() const override
- {
- return true;
- }
-
- #ifdef ARGS_NOEXCEPT
- /// Only for ARGS_NOEXCEPT
- virtual Error GetError() const override
- {
- const auto nargs = NumberOfArguments();
- if (nargs.min > nargs.max)
- {
- return Error::Usage;
- }
-
- const auto matcherError = matcher.GetError();
- if (matcherError != Error::None)
- {
- return matcherError;
- }
-
- return error;
- }
- #endif
-
- /** Defines how many values can be consumed by this option.
- *
- * \return closed interval [min, max]
- */
- virtual Nargs NumberOfArguments() const noexcept = 0;
-
- /** Parse values of this option.
- *
- * \param value Vector of values. It's size must be in NumberOfArguments() interval.
- */
- virtual void ParseValue(const std::vector<std::string> &value) = 0;
- };
-
- /** Base class for value-accepting flag options
- */
- class ValueFlagBase : public FlagBase
- {
- public:
- ValueFlagBase(const std::string &name_, const std::string &help_, Matcher &&matcher_, const bool extraError_ = false) : FlagBase(name_, help_, std::move(matcher_), extraError_) {}
- ValueFlagBase(const std::string &name_, const std::string &help_, Matcher &&matcher_, Options options_) : FlagBase(name_, help_, std::move(matcher_), options_) {}
- virtual ~ValueFlagBase() {}
-
- virtual Nargs NumberOfArguments() const noexcept override
- {
- return 1;
- }
- };
-
- class CompletionFlag : public ValueFlagBase
- {
- public:
- std::vector<std::string> reply;
- size_t cword = 0;
- std::string syntax;
-
- template <typename GroupClass>
- CompletionFlag(GroupClass &group_, Matcher &&matcher_): ValueFlagBase("completion", "completion flag", std::move(matcher_), Options::Hidden)
- {
- group_.AddCompletion(*this);
- }
-
- virtual ~CompletionFlag() {}
-
- virtual Nargs NumberOfArguments() const noexcept override
- {
- return 2;
- }
-
- virtual void ParseValue(const std::vector<std::string> &value_) override
- {
- syntax = value_.at(0);
- std::istringstream(value_.at(1)) >> cword;
- }
-
- /** Get the completion reply
- */
- std::string Get() noexcept
- {
- return detail::Join(reply, "\n");
- }
-
- virtual void Reset() noexcept override
- {
- ValueFlagBase::Reset();
- cword = 0;
- syntax.clear();
- reply.clear();
- }
- };
-
-
- /** Base class for positional options
- */
- class PositionalBase : public NamedBase
- {
- protected:
- bool ready;
-
- public:
- PositionalBase(const std::string &name_, const std::string &help_, Options options_ = {}) : NamedBase(name_, help_, options_), ready(true) {}
- virtual ~PositionalBase() {}
-
- bool Ready()
- {
- return ready;
- }
-
- virtual void ParseValue(const std::string &value_) = 0;
-
- virtual void Reset() noexcept override
- {
- matched = false;
- ready = true;
- #ifdef ARGS_NOEXCEPT
- error = Error::None;
- errorMsg.clear();
- #endif
- }
-
- virtual PositionalBase *GetNextPositional() override
- {
- return Ready() ? this : nullptr;
- }
-
- virtual bool HasPositional() const override
- {
- return true;
- }
-
- virtual std::vector<std::string> GetProgramLine(const HelpParams ¶ms) const override
- {
- return { IsRequired() ? params.proglineRequiredOpen + Name() + params.proglineRequiredClose
- : params.proglineNonrequiredOpen + Name() + params.proglineNonrequiredClose };
- }
-
- virtual void Validate(const std::string &, const std::string &) const override
- {
- if (IsRequired() && !Matched())
- {
- std::ostringstream problem;
- problem << "Option '" << Name() << "' is required";
- #ifdef ARGS_NOEXCEPT
- error = Error::Required;
- errorMsg = problem.str();
- #else
- throw RequiredError(problem.str());
- #endif
- }
- }
- };
-
- /** Class for all kinds of validating groups, including ArgumentParser
- */
- class Group : public Base
- {
- private:
- std::vector<Base*> children;
- std::function<bool(const Group &)> validator;
-
- public:
- /** Default validators
- */
- struct Validators
- {
- static bool Xor(const Group &group)
- {
- return group.MatchedChildren() == 1;
- }
-
- static bool AtLeastOne(const Group &group)
- {
- return group.MatchedChildren() >= 1;
- }
-
- static bool AtMostOne(const Group &group)
- {
- return group.MatchedChildren() <= 1;
- }
-
- static bool All(const Group &group)
- {
- return group.Children().size() == group.MatchedChildren();
- }
-
- static bool AllOrNone(const Group &group)
- {
- return (All(group) || None(group));
- }
-
- static bool AllChildGroups(const Group &group)
- {
- return std::none_of(std::begin(group.Children()), std::end(group.Children()), [](const Base* child) -> bool {
- return child->IsGroup() && !child->Matched();
- });
- }
-
- static bool DontCare(const Group &)
- {
- return true;
- }
-
- static bool CareTooMuch(const Group &)
- {
- return false;
- }
-
- static bool None(const Group &group)
- {
- return group.MatchedChildren() == 0;
- }
- };
- /// If help is empty, this group will not be printed in help output
- Group(const std::string &help_ = std::string(), const std::function<bool(const Group &)> &validator_ = Validators::DontCare, Options options_ = {}) : Base(help_, options_), validator(validator_) {}
- /// If help is empty, this group will not be printed in help output
- Group(Group &group_, const std::string &help_ = std::string(), const std::function<bool(const Group &)> &validator_ = Validators::DontCare, Options options_ = {}) : Base(help_, options_), validator(validator_)
- {
- group_.Add(*this);
- }
- virtual ~Group() {}
-
- /** Append a child to this Group.
- */
- void Add(Base &child)
- {
- children.emplace_back(&child);
- }
-
- /** Get all this group's children
- */
- const std::vector<Base *> &Children() const
- {
- return children;
- }
-
- /** Return the first FlagBase that matches flag, or nullptr
- *
- * \param flag The flag with prefixes stripped
- * \return the first matching FlagBase pointer, or nullptr if there is no match
- */
- virtual FlagBase *Match(const EitherFlag &flag) override
- {
- for (Base *child: Children())
- {
- if (FlagBase *match = child->Match(flag))
- {
- return match;
- }
- }
- return nullptr;
- }
-
- virtual std::vector<FlagBase*> GetAllFlags() override
- {
- std::vector<FlagBase*> res;
- for (Base *child: Children())
- {
- auto childRes = child->GetAllFlags();
- res.insert(res.end(), childRes.begin(), childRes.end());
- }
- return res;
- }
-
- virtual void Validate(const std::string &shortPrefix, const std::string &longPrefix) const override
- {
- for (Base *child: Children())
- {
- child->Validate(shortPrefix, longPrefix);
- }
- }
-
- /** Get the next ready positional, or nullptr if there is none
- *
- * \return the first ready PositionalBase pointer, or nullptr if there is no match
- */
- virtual PositionalBase *GetNextPositional() override
- {
- for (Base *child: Children())
- {
- if (auto next = child->GetNextPositional())
- {
- return next;
- }
- }
- return nullptr;
- }
-
- /** Get whether this has any FlagBase children
- *
- * \return Whether or not there are any FlagBase children
- */
- virtual bool HasFlag() const override
- {
- return std::any_of(Children().begin(), Children().end(), [](Base *child) { return child->HasFlag(); });
- }
-
- /** Get whether this has any PositionalBase children
- *
- * \return Whether or not there are any PositionalBase children
- */
- virtual bool HasPositional() const override
- {
- return std::any_of(Children().begin(), Children().end(), [](Base *child) { return child->HasPositional(); });
- }
-
- /** Get whether this has any Command children
- *
- * \return Whether or not there are any Command children
- */
- virtual bool HasCommand() const override
- {
- return std::any_of(Children().begin(), Children().end(), [](Base *child) { return child->HasCommand(); });
- }
-
- /** Count the number of matched children this group has
- */
- std::vector<Base *>::size_type MatchedChildren() const
- {
- // Cast to avoid warnings from -Wsign-conversion
- return static_cast<std::vector<Base *>::size_type>(
- std::count_if(std::begin(Children()), std::end(Children()), [](const Base *child){return child->Matched();}));
- }
-
- /** Whether or not this group matches validation
- */
- virtual bool Matched() const noexcept override
- {
- return validator(*this);
- }
-
- /** Get validation
- */
- bool Get() const
- {
- return Matched();
- }
-
- /** Get all the child descriptions for help generation
- */
- virtual std::vector<std::tuple<std::string, std::string, unsigned>> GetDescription(const HelpParams ¶ms, const unsigned int indent) const override
- {
- std::vector<std::tuple<std::string, std::string, unsigned int>> descriptions;
-
- // Push that group description on the back if not empty
- unsigned addindent = 0;
- if (!help.empty())
- {
- descriptions.emplace_back(help, "", indent);
- addindent = 1;
- }
-
- for (Base *child: Children())
- {
- if ((child->GetOptions() & Options::HiddenFromDescription) != Options::None)
- {
- continue;
- }
-
- auto groupDescriptions = child->GetDescription(params, indent + addindent);
- descriptions.insert(
- std::end(descriptions),
- std::make_move_iterator(std::begin(groupDescriptions)),
- std::make_move_iterator(std::end(groupDescriptions)));
- }
- return descriptions;
- }
-
- /** Get the names of positional parameters
- */
- virtual std::vector<std::string> GetProgramLine(const HelpParams ¶ms) const override
- {
- std::vector <std::string> names;
- for (Base *child: Children())
- {
- if ((child->GetOptions() & Options::HiddenFromUsage) != Options::None)
- {
- continue;
- }
-
- auto groupNames = child->GetProgramLine(params);
- names.insert(
- std::end(names),
- std::make_move_iterator(std::begin(groupNames)),
- std::make_move_iterator(std::end(groupNames)));
- }
- return names;
- }
-
- virtual std::vector<Command*> GetCommands() override
- {
- std::vector<Command*> res;
- for (const auto &child : Children())
- {
- auto subparsers = child->GetCommands();
- res.insert(std::end(res), std::begin(subparsers), std::end(subparsers));
- }
- return res;
- }
-
- virtual bool IsGroup() const override
- {
- return true;
- }
-
- virtual void Reset() noexcept override
- {
- Base::Reset();
-
- for (auto &child: Children())
- {
- child->Reset();
- }
- #ifdef ARGS_NOEXCEPT
- error = Error::None;
- errorMsg.clear();
- #endif
- }
-
- #ifdef ARGS_NOEXCEPT
- /// Only for ARGS_NOEXCEPT
- virtual Error GetError() const override
- {
- if (error != Error::None)
- {
- return error;
- }
-
- auto it = std::find_if(Children().begin(), Children().end(), [](const Base *child){return child->GetError() != Error::None;});
- if (it == Children().end())
- {
- return Error::None;
- } else
- {
- return (*it)->GetError();
- }
- }
- #endif
-
- };
-
- /** Class for using global options in ArgumentParser.
- */
- class GlobalOptions : public Group
- {
- public:
- GlobalOptions(Group &base, Base &options_) : Group(base, {}, Group::Validators::DontCare, Options::Global)
- {
- Add(options_);
- }
- };
-
- /** Utility class for building subparsers with coroutines/callbacks.
- *
- * Brief example:
- * \code
- * Command command(argumentParser, "command", "my command", [](args::Subparser &s)
- * {
- * // your command flags/positionals
- * s.Parse(); //required
- * //your command code
- * });
- * \endcode
- *
- * For ARGS_NOEXCEPT mode don't forget to check `s.GetError()` after `s.Parse()`
- * and return if it isn't equals to args::Error::None.
- *
- * \sa Command
- */
- class Subparser : public Group
- {
- private:
- std::vector<std::string> args;
- std::vector<std::string> kicked;
- ArgumentParser *parser = nullptr;
- const HelpParams &helpParams;
- const Command &command;
- bool isParsed = false;
-
- public:
- Subparser(std::vector<std::string> args_, ArgumentParser &parser_, const Command &command_, const HelpParams &helpParams_)
- : Group({}, Validators::AllChildGroups), args(std::move(args_)), parser(&parser_), helpParams(helpParams_), command(command_)
- {
- }
-
- Subparser(const Command &command_, const HelpParams &helpParams_) : Group({}, Validators::AllChildGroups), helpParams(helpParams_), command(command_)
- {
- }
-
- Subparser(const Subparser&) = delete;
- Subparser(Subparser&&) = delete;
- Subparser &operator = (const Subparser&) = delete;
- Subparser &operator = (Subparser&&) = delete;
-
- const Command &GetCommand()
- {
- return command;
- }
-
- /** (INTERNAL) Determines whether Parse was called or not.
- */
- bool IsParsed() const
- {
- return isParsed;
- }
-
- /** Continue parsing arguments for new command.
- */
- void Parse();
-
- /** Returns a vector of kicked out arguments.
- *
- * \sa Base::KickOut
- */
- const std::vector<std::string> &KickedOut() const noexcept
- {
- return kicked;
- }
- };
-
- /** Main class for building subparsers.
- *
- * /sa Subparser
- */
- class Command : public Group
- {
- private:
- friend class Subparser;
-
- std::string name;
- std::string help;
- std::string description;
- std::string epilog;
- std::string proglinePostfix;
-
- std::function<void(Subparser&)> parserCoroutine;
- bool commandIsRequired = true;
- Command *selectedCommand = nullptr;
-
- mutable std::vector<std::tuple<std::string, std::string, unsigned>> subparserDescription;
- mutable std::vector<std::string> subparserProgramLine;
- mutable bool subparserHasFlag = false;
- mutable bool subparserHasPositional = false;
- mutable bool subparserHasCommand = false;
- #ifdef ARGS_NOEXCEPT
- mutable Error subparserError = Error::None;
- #endif
- mutable Subparser *subparser = nullptr;
-
- protected:
-
- class RaiiSubparser
- {
- public:
- RaiiSubparser(ArgumentParser &parser_, std::vector<std::string> args_);
- RaiiSubparser(const Command &command_, const HelpParams ¶ms_);
-
- ~RaiiSubparser()
- {
- command.subparser = oldSubparser;
- }
-
- Subparser &Parser()
- {
- return parser;
- }
-
- private:
- const Command &command;
- Subparser parser;
- Subparser *oldSubparser;
- };
-
- Command() = default;
-
- std::function<void(Subparser&)> &GetCoroutine()
- {
- return selectedCommand != nullptr ? selectedCommand->GetCoroutine() : parserCoroutine;
- }
-
- Command &SelectedCommand()
- {
- Command *res = this;
- while (res->selectedCommand != nullptr)
- {
- res = res->selectedCommand;
- }
-
- return *res;
- }
-
- const Command &SelectedCommand() const
- {
- const Command *res = this;
- while (res->selectedCommand != nullptr)
- {
- res = res->selectedCommand;
- }
-
- return *res;
- }
-
- void UpdateSubparserHelp(const HelpParams ¶ms) const
- {
- if (parserCoroutine)
- {
- RaiiSubparser coro(*this, params);
- #ifndef ARGS_NOEXCEPT
- try
- {
- parserCoroutine(coro.Parser());
- }
- catch (args::SubparserError&)
- {
- }
- #else
- parserCoroutine(coro.Parser());
- #endif
- }
- }
-
- public:
- Command(Group &base_, std::string name_, std::string help_, std::function<void(Subparser&)> coroutine_ = {})
- : name(std::move(name_)), help(std::move(help_)), parserCoroutine(std::move(coroutine_))
- {
- base_.Add(*this);
- }
-
- /** The description that appears on the prog line after options
- */
- const std::string &ProglinePostfix() const
- { return proglinePostfix; }
-
- /** The description that appears on the prog line after options
- */
- void ProglinePostfix(const std::string &proglinePostfix_)
- { this->proglinePostfix = proglinePostfix_; }
-
- /** The description that appears above options
- */
- const std::string &Description() const
- { return description; }
- /** The description that appears above options
- */
-
- void Description(const std::string &description_)
- { this->description = description_; }
-
- /** The description that appears below options
- */
- const std::string &Epilog() const
- { return epilog; }
-
- /** The description that appears below options
- */
- void Epilog(const std::string &epilog_)
- { this->epilog = epilog_; }
-
- /** The name of command
- */
- const std::string &Name() const
- { return name; }
-
- /** The description of command
- */
- const std::string &Help() const
- { return help; }
-
- /** If value is true, parser will fail if no command was parsed.
- *
- * Default: true.
- */
- void RequireCommand(bool value)
- { commandIsRequired = value; }
-
- virtual bool IsGroup() const override
- { return false; }
-
- virtual bool Matched() const noexcept override
- { return Base::Matched(); }
-
- operator bool() const noexcept
- { return Matched(); }
-
- void Match() noexcept
- { matched = true; }
-
- void SelectCommand(Command *c) noexcept
- {
- selectedCommand = c;
-
- if (c != nullptr)
- {
- c->Match();
- }
- }
-
- virtual FlagBase *Match(const EitherFlag &flag) override
- {
- if (selectedCommand != nullptr)
- {
- if (auto *res = selectedCommand->Match(flag))
- {
- return res;
- }
-
- for (auto *child: Children())
- {
- if ((child->GetOptions() & Options::Global) != Options::None)
- {
- if (auto *res = child->Match(flag))
- {
- return res;
- }
- }
- }
-
- return nullptr;
- }
-
- if (subparser != nullptr)
- {
- return subparser->Match(flag);
- }
-
- return Matched() ? Group::Match(flag) : nullptr;
- }
-
- virtual std::vector<FlagBase*> GetAllFlags() override
- {
- std::vector<FlagBase*> res;
-
- if (!Matched())
- {
- return res;
- }
-
- for (auto *child: Children())
- {
- if (selectedCommand == nullptr || (child->GetOptions() & Options::Global) != Options::None)
- {
- auto childFlags = child->GetAllFlags();
- res.insert(res.end(), childFlags.begin(), childFlags.end());
- }
- }
-
- if (selectedCommand != nullptr)
- {
- auto childFlags = selectedCommand->GetAllFlags();
- res.insert(res.end(), childFlags.begin(), childFlags.end());
- }
-
- if (subparser != nullptr)
- {
- auto childFlags = subparser->GetAllFlags();
- res.insert(res.end(), childFlags.begin(), childFlags.end());
- }
-
- return res;
- }
-
- virtual PositionalBase *GetNextPositional() override
- {
- if (selectedCommand != nullptr)
- {
- if (auto *res = selectedCommand->GetNextPositional())
- {
- return res;
- }
-
- for (auto *child: Children())
- {
- if ((child->GetOptions() & Options::Global) != Options::None)
- {
- if (auto *res = child->GetNextPositional())
- {
- return res;
- }
- }
- }
-
- return nullptr;
- }
-
- if (subparser != nullptr)
- {
- return subparser->GetNextPositional();
- }
-
- return Matched() ? Group::GetNextPositional() : nullptr;
- }
-
- virtual bool HasFlag() const override
- {
- return subparserHasFlag || Group::HasFlag();
- }
-
- virtual bool HasPositional() const override
- {
- return subparserHasPositional || Group::HasPositional();
- }
-
- virtual bool HasCommand() const override
- {
- return true;
- }
-
- std::vector<std::string> GetCommandProgramLine(const HelpParams ¶ms) const
- {
- UpdateSubparserHelp(params);
-
- auto res = Group::GetProgramLine(params);
- res.insert(res.end(), subparserProgramLine.begin(), subparserProgramLine.end());
-
- if (!params.proglineCommand.empty() && (Group::HasCommand() || subparserHasCommand))
- {
- res.insert(res.begin(), commandIsRequired ? params.proglineCommand : "[" + params.proglineCommand + "]");
- }
-
- if (!Name().empty())
- {
- res.insert(res.begin(), Name());
- }
-
- if ((subparserHasFlag || Group::HasFlag()) && params.showProglineOptions && !params.proglineShowFlags)
- {
- res.push_back(params.proglineOptions);
- }
-
- if (!ProglinePostfix().empty())
- {
- std::string line;
- for (char c : ProglinePostfix())
- {
- if (isspace(c))
- {
- if (!line.empty())
- {
- res.push_back(line);
- line.clear();
- }
-
- if (c == '\n')
- {
- res.push_back("\n");
- }
- }
- else
- {
- line += c;
- }
- }
-
- if (!line.empty())
- {
- res.push_back(line);
- }
- }
-
- return res;
- }
-
- virtual std::vector<std::string> GetProgramLine(const HelpParams ¶ms) const override
- {
- if (!Matched())
- {
- return {};
- }
-
- return GetCommandProgramLine(params);
- }
-
- virtual std::vector<Command*> GetCommands() override
- {
- if (selectedCommand != nullptr)
- {
- return selectedCommand->GetCommands();
- }
-
- if (Matched())
- {
- return Group::GetCommands();
- }
-
- return { this };
- }
-
- virtual std::vector<std::tuple<std::string, std::string, unsigned>> GetDescription(const HelpParams ¶ms, const unsigned int indent) const override
- {
- std::vector<std::tuple<std::string, std::string, unsigned>> descriptions;
- unsigned addindent = 0;
-
- UpdateSubparserHelp(params);
-
- if (!Matched())
- {
- if (params.showCommandFullHelp)
- {
- std::ostringstream s;
- bool empty = true;
- for (const auto &progline: GetCommandProgramLine(params))
- {
- if (!empty)
- {
- s << ' ';
- }
- else
- {
- empty = false;
- }
-
- s << progline;
- }
-
- descriptions.emplace_back(s.str(), "", indent);
- }
- else
- {
- descriptions.emplace_back(Name(), help, indent);
- }
-
- if (!params.showCommandChildren && !params.showCommandFullHelp)
- {
- return descriptions;
- }
-
- addindent = 1;
- }
-
- if (params.showCommandFullHelp && !Matched())
- {
- descriptions.emplace_back("", "", indent + addindent);
- descriptions.emplace_back(Description().empty() ? Help() : Description(), "", indent + addindent);
- descriptions.emplace_back("", "", indent + addindent);
- }
-
- for (Base *child: Children())
- {
- if ((child->GetOptions() & Options::HiddenFromDescription) != Options::None)
- {
- continue;
- }
-
- auto groupDescriptions = child->GetDescription(params, indent + addindent);
- descriptions.insert(
- std::end(descriptions),
- std::make_move_iterator(std::begin(groupDescriptions)),
- std::make_move_iterator(std::end(groupDescriptions)));
- }
-
- for (auto childDescription: subparserDescription)
- {
- std::get<2>(childDescription) += indent + addindent;
- descriptions.push_back(std::move(childDescription));
- }
-
- if (params.showCommandFullHelp && !Matched())
- {
- descriptions.emplace_back("", "", indent + addindent);
- if (!Epilog().empty())
- {
- descriptions.emplace_back(Epilog(), "", indent + addindent);
- descriptions.emplace_back("", "", indent + addindent);
- }
- }
-
- return descriptions;
- }
-
- virtual void Validate(const std::string &shortprefix, const std::string &longprefix) const override
- {
- if (!Matched())
- {
- return;
- }
-
- auto onValidationError = [&]
- {
- std::ostringstream problem;
- problem << "Group validation failed somewhere!";
- #ifdef ARGS_NOEXCEPT
- error = Error::Validation;
- errorMsg = problem.str();
- #else
- throw ValidationError(problem.str());
- #endif
- };
-
- for (Base *child: Children())
- {
- if (child->IsGroup() && !child->Matched())
- {
- onValidationError();
- }
-
- child->Validate(shortprefix, longprefix);
- }
-
- if (subparser != nullptr)
- {
- subparser->Validate(shortprefix, longprefix);
- if (!subparser->Matched())
- {
- onValidationError();
- }
- }
-
- if (selectedCommand == nullptr && commandIsRequired && (Group::HasCommand() || subparserHasCommand))
- {
- std::ostringstream problem;
- problem << "Command is required";
- #ifdef ARGS_NOEXCEPT
- error = Error::Validation;
- errorMsg = problem.str();
- #else
- throw ValidationError(problem.str());
- #endif
- }
- }
-
- virtual void Reset() noexcept override
- {
- Group::Reset();
- selectedCommand = nullptr;
- subparserProgramLine.clear();
- subparserDescription.clear();
- subparserHasFlag = false;
- subparserHasPositional = false;
- subparserHasCommand = false;
- #ifdef ARGS_NOEXCEPT
- subparserError = Error::None;
- #endif
- }
-
- #ifdef ARGS_NOEXCEPT
- /// Only for ARGS_NOEXCEPT
- virtual Error GetError() const override
- {
- if (!Matched())
- {
- return Error::None;
- }
-
- if (error != Error::None)
- {
- return error;
- }
-
- if (subparserError != Error::None)
- {
- return subparserError;
- }
-
- return Group::GetError();
- }
- #endif
- };
-
- /** The main user facing command line argument parser class
- */
- class ArgumentParser : public Command
- {
- friend class Subparser;
-
- private:
- std::string longprefix;
- std::string shortprefix;
-
- std::string longseparator;
-
- std::string terminator;
-
- bool allowJoinedShortValue = true;
- bool allowJoinedLongValue = true;
- bool allowSeparateShortValue = true;
- bool allowSeparateLongValue = true;
-
- CompletionFlag *completion = nullptr;
- bool readCompletion = false;
-
- protected:
- enum class OptionType
- {
- LongFlag,
- ShortFlag,
- Positional
- };
-
- OptionType ParseOption(const std::string &s, bool allowEmpty = false)
- {
- if (s.find(longprefix) == 0 && (allowEmpty || s.length() > longprefix.length()))
- {
- return OptionType::LongFlag;
- }
-
- if (s.find(shortprefix) == 0 && (allowEmpty || s.length() > shortprefix.length()))
- {
- return OptionType::ShortFlag;
- }
-
- return OptionType::Positional;
- }
-
- template <typename It>
- bool Complete(FlagBase &flag, It it, It end)
- {
- auto nextIt = it;
- if (!readCompletion || (++nextIt != end))
- {
- return false;
- }
-
- const auto &chunk = *it;
- for (auto &choice : flag.HelpChoices(helpParams))
- {
- AddCompletionReply(chunk, choice);
- }
-
- #ifndef ARGS_NOEXCEPT
- throw Completion(completion->Get());
- #else
- return true;
- #endif
- }
-
- /** (INTERNAL) Parse flag's values
- *
- * \param arg The string to display in error message as a flag name
- * \param[in, out] it The iterator to first value. It will point to the last value
- * \param end The end iterator
- * \param joinedArg Joined value (e.g. bar in --foo=bar)
- * \param canDiscardJoined If true joined value can be parsed as flag not as a value (as in -abcd)
- * \param[out] values The vector to store parsed arg's values
- */
- template <typename It>
- std::string ParseArgsValues(FlagBase &flag, const std::string &arg, It &it, It end,
- const bool allowSeparate, const bool allowJoined,
- const bool hasJoined, const std::string &joinedArg,
- const bool canDiscardJoined, std::vector<std::string> &values)
- {
- values.clear();
-
- Nargs nargs = flag.NumberOfArguments();
-
- if (hasJoined && !allowJoined && nargs.min != 0)
- {
- return "Flag '" + arg + "' was passed a joined argument, but these are disallowed";
- }
-
- if (hasJoined)
- {
- if (!canDiscardJoined || nargs.max != 0)
- {
- values.push_back(joinedArg);
- }
- } else if (!allowSeparate)
- {
- if (nargs.min != 0)
- {
- return "Flag '" + arg + "' was passed a separate argument, but these are disallowed";
- }
- } else
- {
- auto valueIt = it;
- ++valueIt;
-
- while (valueIt != end &&
- values.size() < nargs.max &&
- (nargs.min == nargs.max || ParseOption(*valueIt) == OptionType::Positional))
- {
- if (Complete(flag, valueIt, end))
- {
- it = end;
- return "";
- }
-
- values.push_back(*valueIt);
- ++it;
- ++valueIt;
- }
- }
-
- if (values.size() > nargs.max)
- {
- return "Passed an argument into a non-argument flag: " + arg;
- } else if (values.size() < nargs.min)
- {
- if (nargs.min == 1 && nargs.max == 1)
- {
- return "Flag '" + arg + "' requires an argument but received none";
- } else if (nargs.min == 1)
- {
- return "Flag '" + arg + "' requires at least one argument but received none";
- } else if (nargs.min != nargs.max)
- {
- return "Flag '" + arg + "' requires at least " + std::to_string(nargs.min) +
- " arguments but received " + std::to_string(values.size());
- } else
- {
- return "Flag '" + arg + "' requires " + std::to_string(nargs.min) +
- " arguments but received " + std::to_string(values.size());
- }
- }
-
- return {};
- }
-
- template <typename It>
- bool ParseLong(It &it, It end)
- {
- const auto &chunk = *it;
- const auto argchunk = chunk.substr(longprefix.size());
- // Try to separate it, in case of a separator:
- const auto separator = longseparator.empty() ? argchunk.npos : argchunk.find(longseparator);
- // If the separator is in the argument, separate it.
- const auto arg = (separator != argchunk.npos ?
- std::string(argchunk, 0, separator)
- : argchunk);
- const auto joined = (separator != argchunk.npos ?
- argchunk.substr(separator + longseparator.size())
- : std::string());
-
- if (auto flag = Match(arg))
- {
- std::vector<std::string> values;
- const std::string errorMessage = ParseArgsValues(*flag, arg, it, end, allowSeparateLongValue, allowJoinedLongValue,
- separator != argchunk.npos, joined, false, values);
- if (!errorMessage.empty())
- {
- #ifndef ARGS_NOEXCEPT
- throw ParseError(errorMessage);
- #else
- error = Error::Parse;
- errorMsg = errorMessage;
- return false;
- #endif
- }
-
- if (!readCompletion)
- {
- flag->ParseValue(values);
- }
-
- if (flag->KickOut())
- {
- ++it;
- return false;
- }
- } else
- {
- const std::string errorMessage("Flag could not be matched: " + arg);
- #ifndef ARGS_NOEXCEPT
- throw ParseError(errorMessage);
- #else
- error = Error::Parse;
- errorMsg = errorMessage;
- return false;
- #endif
- }
-
- return true;
- }
-
- template <typename It>
- bool ParseShort(It &it, It end)
- {
- const auto &chunk = *it;
- const auto argchunk = chunk.substr(shortprefix.size());
- for (auto argit = std::begin(argchunk); argit != std::end(argchunk); ++argit)
- {
- const auto arg = *argit;
-
- if (auto flag = Match(arg))
- {
- const std::string value(argit + 1, std::end(argchunk));
- std::vector<std::string> values;
- const std::string errorMessage = ParseArgsValues(*flag, std::string(1, arg), it, end,
- allowSeparateShortValue, allowJoinedShortValue,
- !value.empty(), value, !value.empty(), values);
-
- if (!errorMessage.empty())
- {
- #ifndef ARGS_NOEXCEPT
- throw ParseError(errorMessage);
- #else
- error = Error::Parse;
- errorMsg = errorMessage;
- return false;
- #endif
- }
-
- if (!readCompletion)
- {
- flag->ParseValue(values);
- }
-
- if (flag->KickOut())
- {
- ++it;
- return false;
- }
-
- if (!values.empty())
- {
- break;
- }
- } else
- {
- const std::string errorMessage("Flag could not be matched: '" + std::string(1, arg) + "'");
- #ifndef ARGS_NOEXCEPT
- throw ParseError(errorMessage);
- #else
- error = Error::Parse;
- errorMsg = errorMessage;
- return false;
- #endif
- }
- }
-
- return true;
- }
-
- bool AddCompletionReply(const std::string &cur, const std::string &choice)
- {
- if (cur.empty() || choice.find(cur) == 0)
- {
- if (completion->syntax == "bash" && ParseOption(choice) == OptionType::LongFlag && choice.find(longseparator) != std::string::npos)
- {
- completion->reply.push_back(choice.substr(choice.find(longseparator) + 1));
- } else
- {
- completion->reply.push_back(choice);
- }
- return true;
- }
-
- return false;
- }
-
- template <typename It>
- bool Complete(It it, It end)
- {
- auto nextIt = it;
- if (!readCompletion || (++nextIt != end))
- {
- return false;
- }
-
- const auto &chunk = *it;
- auto pos = GetNextPositional();
- std::vector<Command *> commands = GetCommands();
- const auto optionType = ParseOption(chunk, true);
-
- if (!commands.empty() && (chunk.empty() || optionType == OptionType::Positional))
- {
- for (auto &cmd : commands)
- {
- if ((cmd->GetOptions() & Options::HiddenFromCompletion) == Options::None)
- {
- AddCompletionReply(chunk, cmd->Name());
- }
- }
- } else
- {
- bool hasPositionalCompletion = true;
-
- if (!commands.empty())
- {
- for (auto &cmd : commands)
- {
- if ((cmd->GetOptions() & Options::HiddenFromCompletion) == Options::None)
- {
- AddCompletionReply(chunk, cmd->Name());
- }
- }
- } else if (pos)
- {
- if ((pos->GetOptions() & Options::HiddenFromCompletion) == Options::None)
- {
- auto choices = pos->HelpChoices(helpParams);
- hasPositionalCompletion = !choices.empty() || optionType != OptionType::Positional;
- for (auto &choice : choices)
- {
- AddCompletionReply(chunk, choice);
- }
- }
- }
-
- if (hasPositionalCompletion)
- {
- auto flags = GetAllFlags();
- for (auto flag : flags)
- {
- if ((flag->GetOptions() & Options::HiddenFromCompletion) != Options::None)
- {
- continue;
- }
-
- auto &matcher = flag->GetMatcher();
- if (!AddCompletionReply(chunk, matcher.GetShortOrAny().str(shortprefix, longprefix)))
- {
- for (auto &flagName : matcher.GetFlagStrings())
- {
- if (AddCompletionReply(chunk, flagName.str(shortprefix, longprefix)))
- {
- break;
- }
- }
- }
- }
-
- if (optionType == OptionType::LongFlag && allowJoinedLongValue)
- {
- const auto separator = longseparator.empty() ? chunk.npos : chunk.find(longseparator);
- if (separator != chunk.npos)
- {
- std::string arg(chunk, 0, separator);
- if (auto flag = this->Match(arg.substr(longprefix.size())))
- {
- for (auto &choice : flag->HelpChoices(helpParams))
- {
- AddCompletionReply(chunk, arg + longseparator + choice);
- }
- }
- }
- } else if (optionType == OptionType::ShortFlag && allowJoinedShortValue)
- {
- if (chunk.size() > shortprefix.size() + 1)
- {
- auto arg = chunk.at(shortprefix.size());
- //TODO: support -abcVALUE where a and b take no value
- if (auto flag = this->Match(arg))
- {
- for (auto &choice : flag->HelpChoices(helpParams))
- {
- AddCompletionReply(chunk, shortprefix + arg + choice);
- }
- }
- }
- }
- }
- }
-
- #ifndef ARGS_NOEXCEPT
- throw Completion(completion->Get());
- #else
- return true;
- #endif
- }
-
- template <typename It>
- It Parse(It begin, It end)
- {
- bool terminated = false;
- std::vector<Command *> commands = GetCommands();
-
- // Check all arg chunks
- for (auto it = begin; it != end; ++it)
- {
- if (Complete(it, end))
- {
- return end;
- }
-
- const auto &chunk = *it;
-
- if (!terminated && chunk == terminator)
- {
- terminated = true;
- } else if (!terminated && ParseOption(chunk) == OptionType::LongFlag)
- {
- if (!ParseLong(it, end))
- {
- return it;
- }
- } else if (!terminated && ParseOption(chunk) == OptionType::ShortFlag)
- {
- if (!ParseShort(it, end))
- {
- return it;
- }
- } else if (!terminated && !commands.empty())
- {
- auto itCommand = std::find_if(commands.begin(), commands.end(), [&chunk](Command *c) { return c->Name() == chunk; });
- if (itCommand == commands.end())
- {
- const std::string errorMessage("Unknown command: " + chunk);
- #ifndef ARGS_NOEXCEPT
- throw ParseError(errorMessage);
- #else
- error = Error::Parse;
- errorMsg = errorMessage;
- return it;
- #endif
- }
-
- SelectedCommand().SelectCommand(*itCommand);
-
- if (const auto &coroutine = GetCoroutine())
- {
- ++it;
- RaiiSubparser coro(*this, std::vector<std::string>(it, end));
- coroutine(coro.Parser());
- #ifdef ARGS_NOEXCEPT
- error = GetError();
- if (error != Error::None)
- {
- return end;
- }
-
- if (!coro.Parser().IsParsed())
- {
- error = Error::Usage;
- return end;
- }
- #else
- if (!coro.Parser().IsParsed())
- {
- throw UsageError("Subparser::Parse was not called");
- }
- #endif
-
- break;
- }
-
- commands = GetCommands();
- } else
- {
- auto pos = GetNextPositional();
- if (pos)
- {
- pos->ParseValue(chunk);
-
- if (pos->KickOut())
- {
- return ++it;
- }
- } else
- {
- const std::string errorMessage("Passed in argument, but no positional arguments were ready to receive it: " + chunk);
- #ifndef ARGS_NOEXCEPT
- throw ParseError(errorMessage);
- #else
- error = Error::Parse;
- errorMsg = errorMessage;
- return it;
- #endif
- }
- }
-
- if (!readCompletion && completion != nullptr && completion->Matched())
- {
- #ifdef ARGS_NOEXCEPT
- error = Error::Completion;
- #endif
- readCompletion = true;
- ++it;
- const auto argsLeft = static_cast<size_t>(std::distance(it, end));
- if (completion->cword == 0 || argsLeft <= 1 || completion->cword >= argsLeft)
- {
- #ifndef ARGS_NOEXCEPT
- throw Completion("");
- #endif
- }
-
- std::vector<std::string> curArgs(++it, end);
- curArgs.resize(completion->cword);
-
- if (completion->syntax == "bash")
- {
- // bash tokenizes --flag=value as --flag=value
- for (size_t idx = 0; idx < curArgs.size(); )
- {
- if (idx > 0 && curArgs[idx] == "=")
- {
- curArgs[idx - 1] += "=";
- // Avoid warnings from -Wsign-conversion
- const auto signedIdx = static_cast<std::ptrdiff_t>(idx);
- if (idx + 1 < curArgs.size())
- {
- curArgs[idx - 1] += curArgs[idx + 1];
- curArgs.erase(curArgs.begin() + signedIdx, curArgs.begin() + signedIdx + 2);
- } else
- {
- curArgs.erase(curArgs.begin() + signedIdx);
- }
- } else
- {
- ++idx;
- }
- }
-
- }
- #ifndef ARGS_NOEXCEPT
- try
- {
- Parse(curArgs.begin(), curArgs.end());
- throw Completion("");
- }
- catch (Completion &)
- {
- throw;
- }
- catch (args::Error&)
- {
- throw Completion("");
- }
- #else
- return Parse(curArgs.begin(), curArgs.end());
- #endif
- }
- }
-
- Validate(shortprefix, longprefix);
- return end;
- }
-
- public:
- HelpParams helpParams;
-
- ArgumentParser(const std::string &description_, const std::string &epilog_ = std::string())
- {
- Description(description_);
- Epilog(epilog_);
- LongPrefix("--");
- ShortPrefix("-");
- LongSeparator("=");
- Terminator("--");
- SetArgumentSeparations(true, true, true, true);
- matched = true;
- }
-
- void AddCompletion(CompletionFlag &completionFlag)
- {
- completion = &completionFlag;
- Add(completionFlag);
- }
-
- /** The program name for help generation
- */
- const std::string &Prog() const
- { return helpParams.programName; }
- /** The program name for help generation
- */
- void Prog(const std::string &prog_)
- { this->helpParams.programName = prog_; }
-
- /** The prefix for long flags
- */
- const std::string &LongPrefix() const
- { return longprefix; }
- /** The prefix for long flags
- */
- void LongPrefix(const std::string &longprefix_)
- {
- this->longprefix = longprefix_;
- this->helpParams.longPrefix = longprefix_;
- }
-
- /** The prefix for short flags
- */
- const std::string &ShortPrefix() const
- { return shortprefix; }
- /** The prefix for short flags
- */
- void ShortPrefix(const std::string &shortprefix_)
- {
- this->shortprefix = shortprefix_;
- this->helpParams.shortPrefix = shortprefix_;
- }
-
- /** The separator for long flags
- */
- const std::string &LongSeparator() const
- { return longseparator; }
- /** The separator for long flags
- */
- void LongSeparator(const std::string &longseparator_)
- {
- if (longseparator_.empty())
- {
- const std::string errorMessage("longseparator can not be set to empty");
- #ifdef ARGS_NOEXCEPT
- error = Error::Usage;
- errorMsg = errorMessage;
- #else
- throw UsageError(errorMessage);
- #endif
- } else
- {
- this->longseparator = longseparator_;
- this->helpParams.longSeparator = allowJoinedLongValue ? longseparator_ : " ";
- }
- }
-
- /** The terminator that forcibly separates flags from positionals
- */
- const std::string &Terminator() const
- { return terminator; }
- /** The terminator that forcibly separates flags from positionals
- */
- void Terminator(const std::string &terminator_)
- { this->terminator = terminator_; }
-
- /** Get the current argument separation parameters.
- *
- * See SetArgumentSeparations for details on what each one means.
- */
- void GetArgumentSeparations(
- bool &allowJoinedShortValue_,
- bool &allowJoinedLongValue_,
- bool &allowSeparateShortValue_,
- bool &allowSeparateLongValue_) const
- {
- allowJoinedShortValue_ = this->allowJoinedShortValue;
- allowJoinedLongValue_ = this->allowJoinedLongValue;
- allowSeparateShortValue_ = this->allowSeparateShortValue;
- allowSeparateLongValue_ = this->allowSeparateLongValue;
- }
-
- /** Change allowed option separation.
- *
- * \param allowJoinedShortValue_ Allow a short flag that accepts an argument to be passed its argument immediately next to it (ie. in the same argv field)
- * \param allowJoinedLongValue_ Allow a long flag that accepts an argument to be passed its argument separated by the longseparator (ie. in the same argv field)
- * \param allowSeparateShortValue_ Allow a short flag that accepts an argument to be passed its argument separated by whitespace (ie. in the next argv field)
- * \param allowSeparateLongValue_ Allow a long flag that accepts an argument to be passed its argument separated by whitespace (ie. in the next argv field)
- */
- void SetArgumentSeparations(
- const bool allowJoinedShortValue_,
- const bool allowJoinedLongValue_,
- const bool allowSeparateShortValue_,
- const bool allowSeparateLongValue_)
- {
- this->allowJoinedShortValue = allowJoinedShortValue_;
- this->allowJoinedLongValue = allowJoinedLongValue_;
- this->allowSeparateShortValue = allowSeparateShortValue_;
- this->allowSeparateLongValue = allowSeparateLongValue_;
-
- this->helpParams.longSeparator = allowJoinedLongValue ? longseparator : " ";
- this->helpParams.shortSeparator = allowJoinedShortValue ? "" : " ";
- }
-
- /** Pass the help menu into an ostream
- */
- void Help(std::ostream &help_) const
- {
- auto &command = SelectedCommand();
- const auto &commandDescription = command.Description().empty() ? command.Help() : command.Description();
- const auto description_text = Wrap(commandDescription, helpParams.width - helpParams.descriptionindent);
- const auto epilog_text = Wrap(command.Epilog(), helpParams.width - helpParams.descriptionindent);
-
- const bool hasoptions = command.HasFlag();
- const bool hasarguments = command.HasPositional();
-
- std::vector<std::string> prognameline;
- prognameline.push_back(helpParams.usageString);
- prognameline.push_back(Prog());
- auto commandProgLine = command.GetProgramLine(helpParams);
- prognameline.insert(prognameline.end(), commandProgLine.begin(), commandProgLine.end());
-
- const auto proglines = Wrap(prognameline.begin(), prognameline.end(),
- helpParams.width - (helpParams.progindent + helpParams.progtailindent),
- helpParams.width - helpParams.progindent);
- auto progit = std::begin(proglines);
- if (progit != std::end(proglines))
- {
- help_ << std::string(helpParams.progindent, ' ') << *progit << '\n';
- ++progit;
- }
- for (; progit != std::end(proglines); ++progit)
- {
- help_ << std::string(helpParams.progtailindent, ' ') << *progit << '\n';
- }
-
- help_ << '\n';
-
- if (!description_text.empty())
- {
- for (const auto &line: description_text)
- {
- help_ << std::string(helpParams.descriptionindent, ' ') << line << "\n";
- }
- help_ << "\n";
- }
-
- bool lastDescriptionIsNewline = false;
-
- if (!helpParams.optionsString.empty())
- {
- help_ << std::string(helpParams.progindent, ' ') << helpParams.optionsString << "\n\n";
- }
-
- for (const auto &desc: command.GetDescription(helpParams, 0))
- {
- lastDescriptionIsNewline = std::get<0>(desc).empty() && std::get<1>(desc).empty();
- const auto groupindent = std::get<2>(desc) * helpParams.eachgroupindent;
- const auto flags = Wrap(std::get<0>(desc), helpParams.width - (helpParams.flagindent + helpParams.helpindent + helpParams.gutter));
- const auto info = Wrap(std::get<1>(desc), helpParams.width - (helpParams.helpindent + groupindent));
-
- std::string::size_type flagssize = 0;
- for (auto flagsit = std::begin(flags); flagsit != std::end(flags); ++flagsit)
- {
- if (flagsit != std::begin(flags))
- {
- help_ << '\n';
- }
- help_ << std::string(groupindent + helpParams.flagindent, ' ') << *flagsit;
- flagssize = Glyphs(*flagsit);
- }
-
- auto infoit = std::begin(info);
- // groupindent is on both sides of this inequality, and therefore can be removed
- if ((helpParams.flagindent + flagssize + helpParams.gutter) > helpParams.helpindent || infoit == std::end(info) || helpParams.addNewlineBeforeDescription)
- {
- help_ << '\n';
- } else
- {
- // groupindent is on both sides of the minus sign, and therefore doesn't actually need to be in here
- help_ << std::string(helpParams.helpindent - (helpParams.flagindent + flagssize), ' ') << *infoit << '\n';
- ++infoit;
- }
- for (; infoit != std::end(info); ++infoit)
- {
- help_ << std::string(groupindent + helpParams.helpindent, ' ') << *infoit << '\n';
- }
- }
- if (hasoptions && hasarguments && helpParams.showTerminator)
- {
- lastDescriptionIsNewline = false;
- for (const auto &item: Wrap(std::string("\"") + terminator + "\" can be used to terminate flag options and force all following arguments to be treated as positional options", helpParams.width - helpParams.flagindent))
- {
- help_ << std::string(helpParams.flagindent, ' ') << item << '\n';
- }
- }
-
- if (!lastDescriptionIsNewline)
- {
- help_ << "\n";
- }
-
- for (const auto &line: epilog_text)
- {
- help_ << std::string(helpParams.descriptionindent, ' ') << line << "\n";
- }
- }
-
- /** Generate a help menu as a string.
- *
- * \return the help text as a single string
- */
- std::string Help() const
- {
- std::ostringstream help_;
- Help(help_);
- return help_.str();
- }
-
- virtual void Reset() noexcept override
- {
- Command::Reset();
- matched = true;
- readCompletion = false;
- }
-
- /** Parse all arguments.
- *
- * \param begin an iterator to the beginning of the argument list
- * \param end an iterator to the past-the-end element of the argument list
- * \return the iterator after the last parsed value. Only useful for kick-out
- */
- template <typename It>
- It ParseArgs(It begin, It end)
- {
- // Reset all Matched statuses and errors
- Reset();
- #ifdef ARGS_NOEXCEPT
- error = GetError();
- if (error != Error::None)
- {
- return end;
- }
- #endif
- return Parse(begin, end);
- }
-
- /** Parse all arguments.
- *
- * \param args an iterable of the arguments
- * \return the iterator after the last parsed value. Only useful for kick-out
- */
- template <typename T>
- auto ParseArgs(const T &args) -> decltype(std::begin(args))
- {
- return ParseArgs(std::begin(args), std::end(args));
- }
-
- /** Convenience function to parse the CLI from argc and argv
- *
- * Just assigns the program name and vectorizes arguments for passing into ParseArgs()
- *
- * \return whether or not all arguments were parsed. This works for detecting kick-out, but is generally useless as it can't do anything with it.
- */
- bool ParseCLI(const int argc, const char * const * argv)
- {
- if (Prog().empty())
- {
- Prog(argv[0]);
- }
- const std::vector<std::string> args(argv + 1, argv + argc);
- return ParseArgs(args) == std::end(args);
- }
-
- template <typename T>
- bool ParseCLI(const T &args)
- {
- return ParseArgs(args) == std::end(args);
- }
- };
-
- inline Command::RaiiSubparser::RaiiSubparser(ArgumentParser &parser_, std::vector<std::string> args_)
- : command(parser_.SelectedCommand()), parser(std::move(args_), parser_, command, parser_.helpParams), oldSubparser(command.subparser)
- {
- command.subparser = &parser;
- }
-
- inline Command::RaiiSubparser::RaiiSubparser(const Command &command_, const HelpParams ¶ms_): command(command_), parser(command, params_), oldSubparser(command.subparser)
- {
- command.subparser = &parser;
- }
-
- inline void Subparser::Parse()
- {
- isParsed = true;
- Reset();
- command.subparserDescription = GetDescription(helpParams, 0);
- command.subparserHasFlag = HasFlag();
- command.subparserHasPositional = HasPositional();
- command.subparserHasCommand = HasCommand();
- command.subparserProgramLine = GetProgramLine(helpParams);
- if (parser == nullptr)
- {
- #ifndef ARGS_NOEXCEPT
- throw args::SubparserError();
- #else
- error = Error::Subparser;
- return;
- #endif
- }
-
- auto it = parser->Parse(args.begin(), args.end());
- command.Validate(parser->ShortPrefix(), parser->LongPrefix());
- kicked.assign(it, args.end());
-
- #ifdef ARGS_NOEXCEPT
- command.subparserError = GetError();
- #endif
- }
-
- inline std::ostream &operator<<(std::ostream &os, const ArgumentParser &parser)
- {
- parser.Help(os);
- return os;
- }
-
- /** Boolean argument matcher
- */
- class Flag : public FlagBase
- {
- public:
- Flag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Options options_): FlagBase(name_, help_, std::move(matcher_), options_)
- {
- group_.Add(*this);
- }
-
- Flag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const bool extraError_ = false): Flag(group_, name_, help_, std::move(matcher_), extraError_ ? Options::Single : Options::None)
- {
- }
-
- virtual ~Flag() {}
-
- /** Get whether this was matched
- */
- bool Get() const
- {
- return Matched();
- }
-
- virtual Nargs NumberOfArguments() const noexcept override
- {
- return 0;
- }
-
- virtual void ParseValue(const std::vector<std::string>&) override
- {
- }
- };
-
- /** Help flag class
- *
- * Works like a regular flag, but throws an instance of Help when it is matched
- */
- class HelpFlag : public Flag
- {
- public:
- HelpFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Options options_ = {}): Flag(group_, name_, help_, std::move(matcher_), options_) {}
-
- virtual ~HelpFlag() {}
-
- virtual void ParseValue(const std::vector<std::string> &)
- {
- #ifdef ARGS_NOEXCEPT
- error = Error::Help;
- errorMsg = Name();
- #else
- throw Help(Name());
- #endif
- }
-
- /** Get whether this was matched
- */
- bool Get() const noexcept
- {
- return Matched();
- }
- };
-
- /** A flag class that simply counts the number of times it's matched
- */
- class CounterFlag : public Flag
- {
- private:
- const int startcount;
- int count;
-
- public:
- CounterFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const int startcount_ = 0, Options options_ = {}):
- Flag(group_, name_, help_, std::move(matcher_), options_), startcount(startcount_), count(startcount_) {}
-
- virtual ~CounterFlag() {}
-
- virtual FlagBase *Match(const EitherFlag &arg) override
- {
- auto me = FlagBase::Match(arg);
- if (me)
- {
- ++count;
- }
- return me;
- }
-
- /** Get the count
- */
- int &Get() noexcept
- {
- return count;
- }
-
- virtual void Reset() noexcept override
- {
- FlagBase::Reset();
- count = startcount;
- }
- };
-
- /** A flag class that calls a function when it's matched
- */
- class ActionFlag : public FlagBase
- {
- private:
- std::function<void(const std::vector<std::string> &)> action;
- Nargs nargs;
-
- public:
- ActionFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Nargs nargs_, std::function<void(const std::vector<std::string> &)> action_, Options options_ = {}):
- FlagBase(name_, help_, std::move(matcher_), options_), action(std::move(action_)), nargs(nargs_)
- {
- group_.Add(*this);
- }
-
- ActionFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, std::function<void(const std::string &)> action_, Options options_ = {}):
- FlagBase(name_, help_, std::move(matcher_), options_), nargs(1)
- {
- group_.Add(*this);
- action = [action_](const std::vector<std::string> &a) { return action_(a.at(0)); };
- }
-
- ActionFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, std::function<void()> action_, Options options_ = {}):
- FlagBase(name_, help_, std::move(matcher_), options_), nargs(0)
- {
- group_.Add(*this);
- action = [action_](const std::vector<std::string> &) { return action_(); };
- }
-
- virtual Nargs NumberOfArguments() const noexcept override
- { return nargs; }
-
- virtual void ParseValue(const std::vector<std::string> &value) override
- { action(value); }
- };
-
- /** A default Reader class for argument classes
- *
- * If destination type is assignable to std::string it uses an assignment to std::string.
- * Otherwise ValueReader simply uses a std::istringstream to read into the destination type, and
- * raises a ParseError if there are any characters left.
- */
- struct ValueReader
- {
- template <typename T>
- typename std::enable_if<!std::is_assignable<T, std::string>::value, bool>::type
- operator ()(const std::string &name, const std::string &value, T &destination)
- {
- std::istringstream ss(value);
- bool failed = !(ss >> destination);
-
- if (!failed)
- {
- ss >> std::ws;
- }
-
- if (ss.rdbuf()->in_avail() > 0 || failed)
- {
- #ifdef ARGS_NOEXCEPT
- (void)name;
- return false;
- #else
- std::ostringstream problem;
- problem << "Argument '" << name << "' received invalid value type '" << value << "'";
- throw ParseError(problem.str());
- #endif
- }
- return true;
- }
-
- template <typename T>
- typename std::enable_if<std::is_assignable<T, std::string>::value, bool>::type
- operator()(const std::string &, const std::string &value, T &destination)
- {
- destination = value;
- return true;
- }
- };
-
- /** An argument-accepting flag class
- *
- * \tparam T the type to extract the argument as
- * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
- */
- template <
- typename T,
- typename Reader = ValueReader>
- class ValueFlag : public ValueFlagBase
- {
- protected:
- T value;
- T defaultValue;
-
- virtual std::string GetDefaultString(const HelpParams&) const override
- {
- return detail::ToString(defaultValue);
- }
-
- private:
- Reader reader;
-
- public:
-
- ValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T &defaultValue_, Options options_): ValueFlagBase(name_, help_, std::move(matcher_), options_), value(defaultValue_), defaultValue(defaultValue_)
- {
- group_.Add(*this);
- }
-
- ValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T &defaultValue_ = T(), const bool extraError_ = false): ValueFlag(group_, name_, help_, std::move(matcher_), defaultValue_, extraError_ ? Options::Single : Options::None)
- {
- }
-
- ValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Options options_): ValueFlag(group_, name_, help_, std::move(matcher_), T(), options_)
- {
- }
-
- virtual ~ValueFlag() {}
-
- virtual void ParseValue(const std::vector<std::string> &values_) override
- {
- const std::string &value_ = values_.at(0);
-
- #ifdef ARGS_NOEXCEPT
- if (!reader(name, value_, this->value))
- {
- error = Error::Parse;
- }
- #else
- reader(name, value_, this->value);
- #endif
- }
-
- virtual void Reset() noexcept override
- {
- ValueFlagBase::Reset();
- value = defaultValue;
- }
-
- /** Get the value
- */
- T &Get() noexcept
- {
- return value;
- }
-
- /** Get the default value
- */
- const T &GetDefault() noexcept
- {
- return defaultValue;
- }
- };
-
- /** An optional argument-accepting flag class
- *
- * \tparam T the type to extract the argument as
- * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
- */
- template <
- typename T,
- typename Reader = ValueReader>
- class ImplicitValueFlag : public ValueFlag<T, Reader>
- {
- protected:
- T implicitValue;
-
- public:
-
- ImplicitValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T &implicitValue_, const T &defaultValue_ = T(), Options options_ = {})
- : ValueFlag<T, Reader>(group_, name_, help_, std::move(matcher_), defaultValue_, options_), implicitValue(implicitValue_)
- {
- }
-
- ImplicitValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const T &defaultValue_ = T(), Options options_ = {})
- : ValueFlag<T, Reader>(group_, name_, help_, std::move(matcher_), defaultValue_, options_), implicitValue(defaultValue_)
- {
- }
-
- ImplicitValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Options options_)
- : ValueFlag<T, Reader>(group_, name_, help_, std::move(matcher_), {}, options_), implicitValue()
- {
- }
-
- virtual ~ImplicitValueFlag() {}
-
- virtual Nargs NumberOfArguments() const noexcept override
- {
- return {0, 1};
- }
-
- virtual void ParseValue(const std::vector<std::string> &value_) override
- {
- if (value_.empty())
- {
- this->value = implicitValue;
- } else
- {
- ValueFlag<T, Reader>::ParseValue(value_);
- }
- }
- };
-
- /** A variadic arguments accepting flag class
- *
- * \tparam T the type to extract the argument as
- * \tparam List the list type that houses the values
- * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
- */
- template <
- typename T,
- template <typename...> class List = std::vector,
- typename Reader = ValueReader>
- class NargsValueFlag : public FlagBase
- {
- protected:
-
- List<T> values;
- const List<T> defaultValues;
- Nargs nargs;
- Reader reader;
-
- public:
-
- typedef List<T> Container;
- typedef T value_type;
- typedef typename Container::allocator_type allocator_type;
- typedef typename Container::pointer pointer;
- typedef typename Container::const_pointer const_pointer;
- typedef T& reference;
- typedef const T& const_reference;
- typedef typename Container::size_type size_type;
- typedef typename Container::difference_type difference_type;
- typedef typename Container::iterator iterator;
- typedef typename Container::const_iterator const_iterator;
- typedef std::reverse_iterator<iterator> reverse_iterator;
- typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
-
- NargsValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Nargs nargs_, const List<T> &defaultValues_ = {}, Options options_ = {})
- : FlagBase(name_, help_, std::move(matcher_), options_), values(defaultValues_), defaultValues(defaultValues_),nargs(nargs_)
- {
- group_.Add(*this);
- }
-
- virtual ~NargsValueFlag() {}
-
- virtual Nargs NumberOfArguments() const noexcept override
- {
- return nargs;
- }
-
- virtual void ParseValue(const std::vector<std::string> &values_) override
- {
- values.clear();
-
- for (const std::string &value : values_)
- {
- T v;
- #ifdef ARGS_NOEXCEPT
- if (!reader(name, value, v))
- {
- error = Error::Parse;
- }
- #else
- reader(name, value, v);
- #endif
- values.insert(std::end(values), v);
- }
- }
-
- List<T> &Get() noexcept
- {
- return values;
- }
-
- iterator begin() noexcept
- {
- return values.begin();
- }
-
- const_iterator begin() const noexcept
- {
- return values.begin();
- }
-
- const_iterator cbegin() const noexcept
- {
- return values.cbegin();
- }
-
- iterator end() noexcept
- {
- return values.end();
- }
-
- const_iterator end() const noexcept
- {
- return values.end();
- }
-
- const_iterator cend() const noexcept
- {
- return values.cend();
- }
-
- virtual void Reset() noexcept override
- {
- FlagBase::Reset();
- values = defaultValues;
- }
-
- virtual FlagBase *Match(const EitherFlag &arg) override
- {
- const bool wasMatched = Matched();
- auto me = FlagBase::Match(arg);
- if (me && !wasMatched)
- {
- values.clear();
- }
- return me;
- }
- };
-
- /** An argument-accepting flag class that pushes the found values into a list
- *
- * \tparam T the type to extract the argument as
- * \tparam List the list type that houses the values
- * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
- */
- template <
- typename T,
- template <typename...> class List = std::vector,
- typename Reader = ValueReader>
- class ValueFlagList : public ValueFlagBase
- {
- private:
- using Container = List<T>;
- Container values;
- const Container defaultValues;
- Reader reader;
-
- public:
-
- typedef T value_type;
- typedef typename Container::allocator_type allocator_type;
- typedef typename Container::pointer pointer;
- typedef typename Container::const_pointer const_pointer;
- typedef T& reference;
- typedef const T& const_reference;
- typedef typename Container::size_type size_type;
- typedef typename Container::difference_type difference_type;
- typedef typename Container::iterator iterator;
- typedef typename Container::const_iterator const_iterator;
- typedef std::reverse_iterator<iterator> reverse_iterator;
- typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
-
- ValueFlagList(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Container &defaultValues_ = Container(), Options options_ = {}):
- ValueFlagBase(name_, help_, std::move(matcher_), options_), values(defaultValues_), defaultValues(defaultValues_)
- {
- group_.Add(*this);
- }
-
- virtual ~ValueFlagList() {}
-
- virtual void ParseValue(const std::vector<std::string> &values_) override
- {
- const std::string &value_ = values_.at(0);
-
- T v;
- #ifdef ARGS_NOEXCEPT
- if (!reader(name, value_, v))
- {
- error = Error::Parse;
- }
- #else
- reader(name, value_, v);
- #endif
- values.insert(std::end(values), v);
- }
-
- /** Get the values
- */
- Container &Get() noexcept
- {
- return values;
- }
-
- virtual std::string Name() const override
- {
- return name + std::string("...");
- }
-
- virtual void Reset() noexcept override
- {
- ValueFlagBase::Reset();
- values = defaultValues;
- }
-
- virtual FlagBase *Match(const EitherFlag &arg) override
- {
- const bool wasMatched = Matched();
- auto me = FlagBase::Match(arg);
- if (me && !wasMatched)
- {
- values.clear();
- }
- return me;
- }
-
- iterator begin() noexcept
- {
- return values.begin();
- }
-
- const_iterator begin() const noexcept
- {
- return values.begin();
- }
-
- const_iterator cbegin() const noexcept
- {
- return values.cbegin();
- }
-
- iterator end() noexcept
- {
- return values.end();
- }
-
- const_iterator end() const noexcept
- {
- return values.end();
- }
-
- const_iterator cend() const noexcept
- {
- return values.cend();
- }
- };
-
- /** A mapping value flag class
- *
- * \tparam K the type to extract the argument as
- * \tparam T the type to store the result as
- * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
- * \tparam Map The Map type. Should operate like std::map or std::unordered_map
- */
- template <
- typename K,
- typename T,
- typename Reader = ValueReader,
- template <typename...> class Map = std::unordered_map>
- class MapFlag : public ValueFlagBase
- {
- private:
- const Map<K, T> map;
- T value;
- const T defaultValue;
- Reader reader;
-
- protected:
- virtual std::vector<std::string> GetChoicesStrings(const HelpParams &) const override
- {
- return detail::MapKeysToStrings(map);
- }
-
- public:
-
- MapFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Map<K, T> &map_, const T &defaultValue_, Options options_): ValueFlagBase(name_, help_, std::move(matcher_), options_), map(map_), value(defaultValue_), defaultValue(defaultValue_)
- {
- group_.Add(*this);
- }
-
- MapFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Map<K, T> &map_, const T &defaultValue_ = T(), const bool extraError_ = false): MapFlag(group_, name_, help_, std::move(matcher_), map_, defaultValue_, extraError_ ? Options::Single : Options::None)
- {
- }
-
- MapFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Map<K, T> &map_, Options options_): MapFlag(group_, name_, help_, std::move(matcher_), map_, T(), options_)
- {
- }
-
- virtual ~MapFlag() {}
-
- virtual void ParseValue(const std::vector<std::string> &values_) override
- {
- const std::string &value_ = values_.at(0);
-
- K key;
- #ifdef ARGS_NOEXCEPT
- if (!reader(name, value_, key))
- {
- error = Error::Parse;
- }
- #else
- reader(name, value_, key);
- #endif
- auto it = map.find(key);
- if (it == std::end(map))
- {
- std::ostringstream problem;
- problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
- #ifdef ARGS_NOEXCEPT
- error = Error::Map;
- errorMsg = problem.str();
- #else
- throw MapError(problem.str());
- #endif
- } else
- {
- this->value = it->second;
- }
- }
-
- /** Get the value
- */
- T &Get() noexcept
- {
- return value;
- }
-
- virtual void Reset() noexcept override
- {
- ValueFlagBase::Reset();
- value = defaultValue;
- }
- };
-
- /** A mapping value flag list class
- *
- * \tparam K the type to extract the argument as
- * \tparam T the type to store the result as
- * \tparam List the list type that houses the values
- * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
- * \tparam Map The Map type. Should operate like std::map or std::unordered_map
- */
- template <
- typename K,
- typename T,
- template <typename...> class List = std::vector,
- typename Reader = ValueReader,
- template <typename...> class Map = std::unordered_map>
- class MapFlagList : public ValueFlagBase
- {
- private:
- using Container = List<T>;
- const Map<K, T> map;
- Container values;
- const Container defaultValues;
- Reader reader;
-
- protected:
- virtual std::vector<std::string> GetChoicesStrings(const HelpParams &) const override
- {
- return detail::MapKeysToStrings(map);
- }
-
- public:
- typedef T value_type;
- typedef typename Container::allocator_type allocator_type;
- typedef typename Container::pointer pointer;
- typedef typename Container::const_pointer const_pointer;
- typedef T& reference;
- typedef const T& const_reference;
- typedef typename Container::size_type size_type;
- typedef typename Container::difference_type difference_type;
- typedef typename Container::iterator iterator;
- typedef typename Container::const_iterator const_iterator;
- typedef std::reverse_iterator<iterator> reverse_iterator;
- typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
-
- MapFlagList(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Map<K, T> &map_, const Container &defaultValues_ = Container()): ValueFlagBase(name_, help_, std::move(matcher_)), map(map_), values(defaultValues_), defaultValues(defaultValues_)
- {
- group_.Add(*this);
- }
-
- virtual ~MapFlagList() {}
-
- virtual void ParseValue(const std::vector<std::string> &values_) override
- {
- const std::string &value = values_.at(0);
-
- K key;
- #ifdef ARGS_NOEXCEPT
- if (!reader(name, value, key))
- {
- error = Error::Parse;
- }
- #else
- reader(name, value, key);
- #endif
- auto it = map.find(key);
- if (it == std::end(map))
- {
- std::ostringstream problem;
- problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
- #ifdef ARGS_NOEXCEPT
- error = Error::Map;
- errorMsg = problem.str();
- #else
- throw MapError(problem.str());
- #endif
- } else
- {
- this->values.emplace_back(it->second);
- }
- }
-
- /** Get the value
- */
- Container &Get() noexcept
- {
- return values;
- }
-
- virtual std::string Name() const override
- {
- return name + std::string("...");
- }
-
- virtual void Reset() noexcept override
- {
- ValueFlagBase::Reset();
- values = defaultValues;
- }
-
- virtual FlagBase *Match(const EitherFlag &arg) override
- {
- const bool wasMatched = Matched();
- auto me = FlagBase::Match(arg);
- if (me && !wasMatched)
- {
- values.clear();
- }
- return me;
- }
-
- iterator begin() noexcept
- {
- return values.begin();
- }
-
- const_iterator begin() const noexcept
- {
- return values.begin();
- }
-
- const_iterator cbegin() const noexcept
- {
- return values.cbegin();
- }
-
- iterator end() noexcept
- {
- return values.end();
- }
-
- const_iterator end() const noexcept
- {
- return values.end();
- }
-
- const_iterator cend() const noexcept
- {
- return values.cend();
- }
- };
-
- /** A positional argument class
- *
- * \tparam T the type to extract the argument as
- * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
- */
- template <
- typename T,
- typename Reader = ValueReader>
- class Positional : public PositionalBase
- {
- private:
- T value;
- const T defaultValue;
- Reader reader;
- public:
- Positional(Group &group_, const std::string &name_, const std::string &help_, const T &defaultValue_ = T(), Options options_ = {}): PositionalBase(name_, help_, options_), value(defaultValue_), defaultValue(defaultValue_)
- {
- group_.Add(*this);
- }
-
- Positional(Group &group_, const std::string &name_, const std::string &help_, Options options_): Positional(group_, name_, help_, T(), options_)
- {
- }
-
- virtual ~Positional() {}
-
- virtual void ParseValue(const std::string &value_) override
- {
- #ifdef ARGS_NOEXCEPT
- if (!reader(name, value_, this->value))
- {
- error = Error::Parse;
- }
- #else
- reader(name, value_, this->value);
- #endif
- ready = false;
- matched = true;
- }
-
- /** Get the value
- */
- T &Get() noexcept
- {
- return value;
- }
-
- virtual void Reset() noexcept override
- {
- PositionalBase::Reset();
- value = defaultValue;
- }
- };
-
- /** A positional argument class that pushes the found values into a list
- *
- * \tparam T the type to extract the argument as
- * \tparam List the list type that houses the values
- * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
- */
- template <
- typename T,
- template <typename...> class List = std::vector,
- typename Reader = ValueReader>
- class PositionalList : public PositionalBase
- {
- private:
- using Container = List<T>;
- Container values;
- const Container defaultValues;
- Reader reader;
-
- public:
- typedef T value_type;
- typedef typename Container::allocator_type allocator_type;
- typedef typename Container::pointer pointer;
- typedef typename Container::const_pointer const_pointer;
- typedef T& reference;
- typedef const T& const_reference;
- typedef typename Container::size_type size_type;
- typedef typename Container::difference_type difference_type;
- typedef typename Container::iterator iterator;
- typedef typename Container::const_iterator const_iterator;
- typedef std::reverse_iterator<iterator> reverse_iterator;
- typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
-
- PositionalList(Group &group_, const std::string &name_, const std::string &help_, const Container &defaultValues_ = Container(), Options options_ = {}): PositionalBase(name_, help_, options_), values(defaultValues_), defaultValues(defaultValues_)
- {
- group_.Add(*this);
- }
-
- PositionalList(Group &group_, const std::string &name_, const std::string &help_, Options options_): PositionalList(group_, name_, help_, {}, options_)
- {
- }
-
- virtual ~PositionalList() {}
-
- virtual void ParseValue(const std::string &value_) override
- {
- T v;
- #ifdef ARGS_NOEXCEPT
- if (!reader(name, value_, v))
- {
- error = Error::Parse;
- }
- #else
- reader(name, value_, v);
- #endif
- values.insert(std::end(values), v);
- matched = true;
- }
-
- virtual std::string Name() const override
- {
- return name + std::string("...");
- }
-
- /** Get the values
- */
- Container &Get() noexcept
- {
- return values;
- }
-
- virtual void Reset() noexcept override
- {
- PositionalBase::Reset();
- values = defaultValues;
- }
-
- virtual PositionalBase *GetNextPositional() override
- {
- const bool wasMatched = Matched();
- auto me = PositionalBase::GetNextPositional();
- if (me && !wasMatched)
- {
- values.clear();
- }
- return me;
- }
-
- iterator begin() noexcept
- {
- return values.begin();
- }
-
- const_iterator begin() const noexcept
- {
- return values.begin();
- }
-
- const_iterator cbegin() const noexcept
- {
- return values.cbegin();
- }
-
- iterator end() noexcept
- {
- return values.end();
- }
-
- const_iterator end() const noexcept
- {
- return values.end();
- }
-
- const_iterator cend() const noexcept
- {
- return values.cend();
- }
- };
-
- /** A positional argument mapping class
- *
- * \tparam K the type to extract the argument as
- * \tparam T the type to store the result as
- * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
- * \tparam Map The Map type. Should operate like std::map or std::unordered_map
- */
- template <
- typename K,
- typename T,
- typename Reader = ValueReader,
- template <typename...> class Map = std::unordered_map>
- class MapPositional : public PositionalBase
- {
- private:
- const Map<K, T> map;
- T value;
- const T defaultValue;
- Reader reader;
-
- protected:
- virtual std::vector<std::string> GetChoicesStrings(const HelpParams &) const override
- {
- return detail::MapKeysToStrings(map);
- }
-
- public:
-
- MapPositional(Group &group_, const std::string &name_, const std::string &help_, const Map<K, T> &map_, const T &defaultValue_ = T(), Options options_ = {}):
- PositionalBase(name_, help_, options_), map(map_), value(defaultValue_), defaultValue(defaultValue_)
- {
- group_.Add(*this);
- }
-
- virtual ~MapPositional() {}
-
- virtual void ParseValue(const std::string &value_) override
- {
- K key;
- #ifdef ARGS_NOEXCEPT
- if (!reader(name, value_, key))
- {
- error = Error::Parse;
- }
- #else
- reader(name, value_, key);
- #endif
- auto it = map.find(key);
- if (it == std::end(map))
- {
- std::ostringstream problem;
- problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
- #ifdef ARGS_NOEXCEPT
- error = Error::Map;
- errorMsg = problem.str();
- #else
- throw MapError(problem.str());
- #endif
- } else
- {
- this->value = it->second;
- ready = false;
- matched = true;
- }
- }
-
- /** Get the value
- */
- T &Get() noexcept
- {
- return value;
- }
-
- virtual void Reset() noexcept override
- {
- PositionalBase::Reset();
- value = defaultValue;
- }
- };
-
- /** A positional argument mapping list class
- *
- * \tparam K the type to extract the argument as
- * \tparam T the type to store the result as
- * \tparam List the list type that houses the values
- * \tparam Reader The functor type used to read the argument, taking the name, value, and destination reference with operator(), and returning a bool (if ARGS_NOEXCEPT is defined)
- * \tparam Map The Map type. Should operate like std::map or std::unordered_map
- */
- template <
- typename K,
- typename T,
- template <typename...> class List = std::vector,
- typename Reader = ValueReader,
- template <typename...> class Map = std::unordered_map>
- class MapPositionalList : public PositionalBase
- {
- private:
- using Container = List<T>;
-
- const Map<K, T> map;
- Container values;
- const Container defaultValues;
- Reader reader;
-
- protected:
- virtual std::vector<std::string> GetChoicesStrings(const HelpParams &) const override
- {
- return detail::MapKeysToStrings(map);
- }
-
- public:
- typedef T value_type;
- typedef typename Container::allocator_type allocator_type;
- typedef typename Container::pointer pointer;
- typedef typename Container::const_pointer const_pointer;
- typedef T& reference;
- typedef const T& const_reference;
- typedef typename Container::size_type size_type;
- typedef typename Container::difference_type difference_type;
- typedef typename Container::iterator iterator;
- typedef typename Container::const_iterator const_iterator;
- typedef std::reverse_iterator<iterator> reverse_iterator;
- typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
-
- MapPositionalList(Group &group_, const std::string &name_, const std::string &help_, const Map<K, T> &map_, const Container &defaultValues_ = Container(), Options options_ = {}):
- PositionalBase(name_, help_, options_), map(map_), values(defaultValues_), defaultValues(defaultValues_)
- {
- group_.Add(*this);
- }
-
- virtual ~MapPositionalList() {}
-
- virtual void ParseValue(const std::string &value_) override
- {
- K key;
- #ifdef ARGS_NOEXCEPT
- if (!reader(name, value_, key))
- {
- error = Error::Parse;
- }
- #else
- reader(name, value_, key);
- #endif
- auto it = map.find(key);
- if (it == std::end(map))
- {
- std::ostringstream problem;
- problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
- #ifdef ARGS_NOEXCEPT
- error = Error::Map;
- errorMsg = problem.str();
- #else
- throw MapError(problem.str());
- #endif
- } else
- {
- this->values.emplace_back(it->second);
- matched = true;
- }
- }
-
- /** Get the value
- */
- Container &Get() noexcept
- {
- return values;
- }
-
- virtual std::string Name() const override
- {
- return name + std::string("...");
- }
-
- virtual void Reset() noexcept override
- {
- PositionalBase::Reset();
- values = defaultValues;
- }
-
- virtual PositionalBase *GetNextPositional() override
- {
- const bool wasMatched = Matched();
- auto me = PositionalBase::GetNextPositional();
- if (me && !wasMatched)
- {
- values.clear();
- }
- return me;
- }
-
- iterator begin() noexcept
- {
- return values.begin();
- }
-
- const_iterator begin() const noexcept
- {
- return values.begin();
- }
-
- const_iterator cbegin() const noexcept
- {
- return values.cbegin();
- }
-
- iterator end() noexcept
- {
- return values.end();
- }
-
- const_iterator end() const noexcept
- {
- return values.end();
- }
-
- const_iterator cend() const noexcept
- {
- return values.cend();
- }
- };
- }
-
- #endif
|