summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsteven.bethard <devnull@localhost>2010-01-07 17:30:28 +0000
committersteven.bethard <devnull@localhost>2010-01-07 17:30:28 +0000
commit6f0470065b4aa7c8673ba375b4d99b8809999fb5 (patch)
treed45ab818ba62dcc8239546401304537d1f70c5ab
parent595bce353fc65c44168df31a9cd3e29b9059b486 (diff)
downloadargparse-6f0470065b4aa7c8673ba375b4d99b8809999fb5.tar.gz
Add convert_arg_line_to_args to allow subclasses to easily override file parsing behavior.
-rw-r--r--argparse.py8
-rw-r--r--doc/ArgumentParser.html2
-rw-r--r--doc/api-docs.html1
-rw-r--r--doc/genindex.html9
-rw-r--r--doc/other-methods.html19
-rw-r--r--doc/searchindex.js2
-rw-r--r--doc/source/ArgumentParser.rst3
-rw-r--r--doc/source/other-methods.rst16
-rw-r--r--test/test_argparse.py34
9 files changed, 87 insertions, 7 deletions
diff --git a/argparse.py b/argparse.py
index 8b96e62..95fdd72 100644
--- a/argparse.py
+++ b/argparse.py
@@ -1971,7 +1971,10 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
try:
args_file = open(arg_string[1:])
try:
- arg_strings = args_file.read().splitlines()
+ arg_strings = []
+ for arg_line in args_file.read().splitlines():
+ for arg in self.convert_arg_line_to_args(arg_line):
+ arg_strings.append(arg)
arg_strings = self._read_args_from_files(arg_strings)
new_arg_strings.extend(arg_strings)
finally:
@@ -1983,6 +1986,9 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
# return the modified argument list
return new_arg_strings
+ def convert_arg_line_to_args(self, arg_line):
+ return [arg_line]
+
def _match_argument(self, action, arg_strings_pattern):
# match the pattern for this action to the arg strings
nargs_pattern = self._get_nargs_pattern(action)
diff --git a/doc/ArgumentParser.html b/doc/ArgumentParser.html
index c0ddeb8..fc8df9d 100644
--- a/doc/ArgumentParser.html
+++ b/doc/ArgumentParser.html
@@ -172,7 +172,7 @@ If the <tt class="docutils literal"><span class="pre">fromfile_prefix_chars=</sp
<span class="go">Namespace(f=&#39;bar&#39;)</span>
</pre></div>
</div>
-<p>Arguments read from a file must be one per line (with each whole line being considered a single argument) and are treated as if they were in the same place as the original file referencing argument on the command line.
+<p>Arguments read from a file must by default be one per line (but see also <a title="convert_arg_line_to_args" class="reference external" href="other-methods.html#convert_arg_line_to_args"><tt class="xref docutils literal"><span class="pre">convert_arg_line_to_args()</span></tt></a>) and are treated as if they were in the same place as the original file referencing argument on the command line.
So in the example above, the expression <tt class="docutils literal"><span class="pre">['-f',</span> <span class="pre">'foo',</span> <span class="pre">'&#64;args.txt']</span></tt> is considered equivalent to the expression <tt class="docutils literal"><span class="pre">['-f',</span> <span class="pre">'foo',</span> <span class="pre">'-f',</span> <span class="pre">'bar']</span></tt>.</p>
<p>The <tt class="docutils literal"><span class="pre">fromfile_prefix_chars=</span></tt> argument defaults to <tt class="xref docutils literal"><span class="pre">None</span></tt>, meaning that arguments will never be treated as file references.</p>
</div>
diff --git a/doc/api-docs.html b/doc/api-docs.html
index 227e0da..eadfbe0 100644
--- a/doc/api-docs.html
+++ b/doc/api-docs.html
@@ -93,6 +93,7 @@
<li class="toctree-l2"><a class="reference external" href="other-methods.html#sub-commands">Sub-commands</a></li>
<li class="toctree-l2"><a class="reference external" href="other-methods.html#argument-groups">Argument groups</a></li>
<li class="toctree-l2"><a class="reference external" href="other-methods.html#mutual-exclusion">Mutual exclusion</a></li>
+<li class="toctree-l2"><a class="reference external" href="other-methods.html#customizing-file-parsing">Customizing file parsing</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference external" href="other-utilities.html">Other utilities</a><ul>
diff --git a/doc/genindex.html b/doc/genindex.html
index 238b203..96078b3 100644
--- a/doc/genindex.html
+++ b/doc/genindex.html
@@ -40,7 +40,7 @@
<h1 id="index">Index</h1>
- <a href="#A"><strong>A</strong></a> | <a href="#F"><strong>F</strong></a> | <a href="#G"><strong>G</strong></a> | <a href="#P"><strong>P</strong></a> | <a href="#S"><strong>S</strong></a>
+ <a href="#A"><strong>A</strong></a> | <a href="#C"><strong>C</strong></a> | <a href="#F"><strong>F</strong></a> | <a href="#G"><strong>G</strong></a> | <a href="#P"><strong>P</strong></a> | <a href="#S"><strong>S</strong></a>
<hr />
@@ -56,6 +56,13 @@
<dt><a href="ArgumentParser.html#ArgumentParser">ArgumentParser (built-in class)</a></dt>
</dl></td></tr></table>
+<h2 id="C">C</h2>
+<table width="100%" class="indextable"><tr><td width="33%" valign="top">
+<dl>
+
+<dt><a href="other-methods.html#convert_arg_line_to_args">convert_arg_line_to_args()</a></dt></dl></td><td width="33%" valign="top"><dl>
+</dl></td></tr></table>
+
<h2 id="F">F</h2>
<table width="100%" class="indextable"><tr><td width="33%" valign="top">
<dl>
diff --git a/doc/other-methods.html b/doc/other-methods.html
index 2b33b0d..033240d 100644
--- a/doc/other-methods.html
+++ b/doc/other-methods.html
@@ -330,6 +330,24 @@ Instead, it returns a two item tuple containing the populated namespace and the
</dd></dl>
</div>
+<div class="section" id="customizing-file-parsing">
+<h2>Customizing file parsing<a class="headerlink" href="#customizing-file-parsing" title="Permalink to this headline">¶</a></h2>
+<dl class="method">
+<dt id="convert_arg_line_to_args">
+<tt class="descname">convert_arg_line_to_args</tt><big>(</big><em>arg_line</em><big>)</big><a class="headerlink" href="#convert_arg_line_to_args" title="Permalink to this definition">¶</a></dt>
+<dd><p>Arguments that are read from a file (see the <tt class="docutils literal"><span class="pre">fromfile_prefix_chars</span></tt> keyword argument to the <a title="ArgumentParser" class="reference external" href="ArgumentParser.html#ArgumentParser"><tt class="xref docutils literal"><span class="pre">ArgumentParser</span></tt></a> constructor) are read one argument per line. If you need fancier parsing, then you can subclass the <a title="ArgumentParser" class="reference external" href="ArgumentParser.html#ArgumentParser"><tt class="xref docutils literal"><span class="pre">ArgumentParser</span></tt></a> and override the <a title="convert_arg_line_to_args" class="reference internal" href="#convert_arg_line_to_args"><tt class="xref docutils literal"><span class="pre">convert_arg_line_to_args()</span></tt></a> method.</p>
+<p>This method takes a single argument <tt class="docutils literal"><span class="pre">arg_line</span></tt> which is a string read from the argument file. It returns a list of arguments parsed from this string. The method is called once per line read from the argument file, in order.</p>
+<p>A useful override of this method is one that treats each space-separated word as an argument:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">convert_arg_line_to_args</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg_line</span><span class="p">):</span>
+ <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">arg_line</span><span class="o">.</span><span class="n">split</span><span class="p">():</span>
+ <span class="k">if</span> <span class="ow">not</span> <span class="n">arg</span><span class="o">.</span><span class="n">strip</span><span class="p">():</span>
+ <span class="k">continue</span>
+ <span class="k">yield</span> <span class="n">arg</span>
+</pre></div>
+</div>
+</dd></dl>
+
+</div>
</div>
@@ -347,6 +365,7 @@ Instead, it returns a two item tuple containing the populated namespace and the
<li><a class="reference external" href="#sub-commands">Sub-commands</a></li>
<li><a class="reference external" href="#argument-groups">Argument groups</a></li>
<li><a class="reference external" href="#mutual-exclusion">Mutual exclusion</a></li>
+<li><a class="reference external" href="#customizing-file-parsing">Customizing file parsing</a></li>
</ul>
</li>
</ul>
diff --git a/doc/searchindex.js b/doc/searchindex.js
index bb98dfa..3a561be 100644
--- a/doc/searchindex.js
+++ b/doc/searchindex.js
@@ -1 +1 @@
-Search.setIndex({desctypes:{"0":"method","1":"class"},terms:{all:[6,3,7,5,2],code:[0,7,6],partial:[8,7,2],messg:7,whatev:2,illustr:[7,5],global:6,four:5,prefix:[1,6,5],upgrad:[0,7],follow:[0,7,5,6,3],formatter_class:[8,6],whose:[5,6],typeerror:[],"const":[8,3,7,1,5],readabl:4,prefix_char:[8,7,6],program:[6,0,5,2,3],present:[7,1,2,5],sens:[5,6],worth:5,consum:[7,5],everi:3,string:[1,2,3,5,6,7],fals:[6,7,5,2],subcommand:[7,2],util:[8,0,4],parent_pars:6,retriev:[],default_:[],exact:7,implement:5,level:2,list:[1,2,3,5,6,7],"try":[7,6],item:[5,2],adjust:6,stderr:2,pleas:6,subparser_nam:2,bacon:1,dogmat:7,past:7,zero:[7,5],fyoyo:7,group1:2,pass:[1,2,4,5,6,7],append:5,compat:7,what:[3,7,1,6,5],abc:[7,5],sub:[8,7,2],neg:1,section:[6,7,5,2],advanc:7,brief:[6,5,2],current:[6,2],abbrevi:[8,1],version:[8,5,6],"new":[7,1,6,5],method:[0,1,2,3,5,6,7,8],told:5,deriv:[7,5],gener:[0,7,5,6,3],never:[7,6],here:[7,1],let:2,inher:1,path:7,along:1,infil:[4,5],modifi:2,sinc:7,interpret:5,convert:[3,4,7,1,5],produc:[7,5,2],convers:5,pick:1,action:[1,2,3,5,6,7,8],weird:6,commonli:5,mutual:[8,2],control:6,regardless:6,extra:[1,2],appli:5,modul:[0,7,5,3],prefer:7,filenam:5,api:[8,0,7,5],txt:[0,5,6],select:[5,2],add_argument_group:2,from:[1,2,3,5,6,7],describ:[6,5,2],would:[7,5],frobbl:[5,2],regist:2,two:[6,3,1,2,5],few:[7,5,2],call:[0,1,2,3,5,6,7],add_help:[8,7,6,2],recommend:2,taken:[7,1,5],rawtexthelpformatt:6,type:[0,1,2,3,4,5,6,7,8],tell:[3,1],more:[0,1,3,5,6,7],sort:[3,5,6],parser_a:2,parser_b:2,notic:[7,1],warn:5,flag:[8,7,5],indic:[6,5,2],particular:[7,5,2],known:7,hold:[3,5],easiest:[7,5,2],must:[1,6,5],dictat:7,none:[4,7,1,6,5],word:6,dest:[1,2,3,5,7,8],work:[6,2],conceptu:2,remain:[3,7,5,2],itself:5,can:[0,1,2,3,4,5,6,7],def:[7,5,2],overrid:[7,6,2],traceback:6,prompt:1,puriti:7,give:[1,6],frabbl:3,share:[7,6],add_argu:[0,1,2,3,4,5,6,7,8],accept:[7,5,2],cautiou:1,want:[6,3,7,5,2],alwai:[6,5,2],multipl:[7,5,2],thing:5,rather:[1,6],anoth:[6,5,2],check_method:7,fooaction:[7,5],write:[0,7,6,3],how:[6,3,1,2,5],sever:[6,1,2],instead:[7,5,2],narg:[0,1,3,5,6,7,8],simpl:[0,7,5,3],updat:2,parser_abc:7,foo_pars:[7,6],overridden:6,mess:6,max:[3,1],after:[6,7,1,2,5],variant:2,invok:[6,3,7,5,2],befor:[5,6],wrong:1,okai:6,mai:[6,7,1,2,5],end:[7,5,2],associ:[5,2],xfoox:7,"0x013a2410":[],"short":[1,6,5],attempt:[7,1],practic:7,stdin:[4,5],explicit:5,correspond:[3,7],ambigu:1,caus:[5,6],callback:7,type_:[],maintain:[7,6],combin:[1,2],allow:[6,7,1,2,5],callabl:[7,5],origin:[7,6],least:[5,2],help:[0,1,2,3,5,6,7,8],over:[7,6],becaus:6,through:7,same:[7,5,6],hierarchi:7,metavar_:[],still:5,subparser2:2,subparser1:2,paramet:[7,5,6],perfect:5,style:1,interspers:7,group:[8,2],fit:6,better:[7,2],main:2,easier:1,them:[0,5,1,6,3],good:2,"return":[3,7,1,2,5],greater:6,thei:[7,1,6,5],python:[0,7,6,2],initi:[3,5],option_str:[7,5],now:[3,7],discuss:7,introduct:[0,3],choic:[1,2,3,5,7,8],allow_interspersed_arg:7,optionerror:7,name:[1,2,3,5,6,7,8],anyth:[7,5],choices_:[],separ:[1,2],easili:7,achiev:[1,6],mode:[4,7,5],each:[6,3,7,5,2],fulli:2,difficult:7,nbar:6,mean:[3,7,5,6],myprogram:6,replac:[7,6],idea:2,heavi:1,expect:[3,1,5],our:3,happen:7,beyond:[8,1],metavar:[0,1,3,5,7,8],special:[5,2],out:[0,4,5,6,3],variabl:7,accomplish:[1,6],referenc:6,space:6,foo_bar:5,newli:1,parser_xyz:7,perfect_squar:5,print:[1,2,3,5,6,7,8],factori:[4,5],math:5,common:[3,5,6],optionvalueerror:7,worthwhil:7,situat:[1,5],given:6,argv:[8,3,1,6],standard:7,mmm:1,reason:7,base:7,dictionari:7,care:[7,2],indent:6,could:[1,5],omit:5,refus:7,keep:6,fromfile_prefix_char:[8,6],turn:3,place:6,isn:[7,5],retain:6,const_:[],assign:1,first:[5,6],oper:5,rang:[3,1],directli:5,carri:3,onc:3,number:[6,7,1,2,5],restrict:5,instruct:3,alreadi:[7,1,6],construct:[7,6],wasn:5,open:[4,7,5,6],size:4,differ:[7,5,2],script:[3,2],top:2,sometim:[6,1,2],messag:[0,1,2,5,6,7],too:5,similarli:2,conveni:[7,5],"final":[],store:[3,5],monkei:7,option:[0,1,2,3,5,6,7,8],namespac:[1,2,3,4,5,6,7,8],copi:7,specifi:[0,1,2,3,5,6,7],pars:[0,1,2,3,5,7,8],store_tru:[3,7,1,2,5],exactli:[3,1,6],than:[6,7,1,2,5],format_usag:2,wide:6,kind:2,setattr:[7,5],whenev:[1,5],provid:[0,7,1,2,5],remov:6,charact:[7,1,6,5],str:5,were:[5,6],posit:[0,1,2,3,5,6,7],seri:[3,5],sai:7,abov:[6,0,1,2,3],"0x013a2380":[],group2:2,argument:[0,1,2,3,4,5,6,7,8],dash:7,add_pars:[7,2],manner:[3,7],have:[4,7,1,6,5],"__main__":[0,3],need:[6,7,5,2],seem:7,squar:5,equival:6,inform:[6,3,7,5,2],self:[7,5],append_const:5,also:[6,1,2,5],builtin:5,exampl:[0,1,2,3,5,6],take:[1,2,3,5,6,7],which:[6,7,1,2,5],singl:[1,2,3,5,6,7],uppercas:5,begin:[3,1],sure:[7,5,2],though:5,buffer:4,previou:[7,1],most:[6,3,7,5,2],regular:2,pair:[7,6],choos:[7,5],"class":[4,7,1,6,5],appear:2,don:[7,1,5],later:3,request:[4,5,2],doe:[6,7,5,2],declar:7,determin:[1,2,3,5,6,7],occasion:6,sum:[0,1,3],"0x00b8fb18":[],parser_bar:2,show:[6,0,7,5,2],text:6,filetyp:[8,0,4,5],syntax:[8,7,1],particularli:[7,6,2],hack:7,find:[3,1,2],onli:[6,7,1,2,5],textual:6,parse_known_arg:2,just:[5,2],pretti:3,"true":[1,2,3,5,6,7],parser_foo:2,figur:5,should:[0,2,3,5,6,7],store_const:[3,7,1,5],wood:1,dict:5,"__call__":[7,5],add_opt:7,variou:[5,6],get:[6,3,7,1,2],express:6,report:5,requir:[8,7,1,2,5],bar:[1,2,3,5,6,7],keyword:[1,2,3,5,6,7],baz:[7,5,2],dramat:7,patch:7,store_fals:[5,2],whether:[5,6],bad:[1,5],calcul:6,bac:1,contain:[1,2,5,6,7,8],where:[0,5,6],set:[6,3,7,5,2],"float":[7,5,2],see:[1,2,3,5,6,7],arg:[0,1,2,3,4,5,6,7],close:0,extend:[7,5],correctli:6,someth:3,written:0,subdir:6,between:6,"import":[0,7,5,6,3],awai:5,badger:[7,1,2],across:6,attribut:[1,2,3,5,6,7],altern:6,parent:[8,7,6,2],xrang:[3,7,1,5],disallow:[7,6],extens:7,outfil:5,come:7,addit:[6,7,5,2],both:[7,1],last:[1,6],howev:[6,7,5,2],against:7,etc:[7,1,2,5],eas:5,mani:[7,5],whole:6,simpli:[6,7,1,2,5],point:7,argumentpars:[0,1,2,3,4,5,6,7,8],dispatch:7,featur:7,suppli:[6,3,7,5,2],respect:5,assum:[0,5,2,3],duplic:7,quit:[7,5],coupl:[6,2],addition:5,three:6,empti:1,implicit:7,accumul:[3,1],secret:7,much:[3,7,1,2,5],valu:[1,2,5,6,7,8],basic:5,unambigu:1,print_usag:2,popul:[1,2],strategi:6,epilog:[8,6],suppress:[7,5,6],xxx:[7,5,6],great:5,ani:[6,7,5,2],understand:4,togeth:[1,5],func:2,child:6,repetit:5,those:[7,1],"case":[3,1,2,5],therefor:5,look:[3,7,1],format_help:2,formatt:6,defin:[6,5,2],"while":[7,1,6,5],behavior:[3,5,6],error:[6,7,1,2,5],argpars:[0,1,2,3,4,5,6,7],advantag:[0,7],stdout:[0,4,5],almost:[5,6],kwarg:[7,2],"__init__":7,clearli:1,perform:[5,2],make:[6,3,7,5,2],format:[6,7,5,2],sqrt:5,check:[7,1,2,5],handl:[7,5,2],complex:[7,5],help_:[],split:[1,2,3,5,6,7],document:[8,0,1,6],infer:5,dest_:[],complet:2,dedent:6,foo_bar_baz_pars:7,effect:2,rais:[5,6],user:[6,7,1,2,5],store_act:7,typic:[3,7,5,2],recent:6,appropri:[0,2,3,5,6,7],older:6,argumenttypeerror:5,thu:7,inherit:[7,6],likewis:6,without:2,command:[0,1,2,3,4,5,6,7,8],thi:[0,1,2,3,4,5,6,7],conflict:6,everyth:[7,1],sibl:2,usual:[6,7,5,2],identifi:5,execut:2,add_mutually_exclusive_group:2,note:[6,7,5,2],action_:[],exclus:[8,2],expos:7,had:7,except:[6,2],add:[0,2,3,5,6,7],valid:[7,5,2],remaining_arg:7,rawdescriptionhelpformatt:6,save:[0,3],match:[6,7,1,2,5],applic:[6,2],transpar:7,read:[5,6],textwrap:6,writabl:[4,5],built:[3,1],know:[7,5,2],print_help:[6,7,5,2],like:[1,2,3,5,6,7],insert:[1,6],get_default:2,specif:6,arbitrari:5,whitespac:6,manual:7,resolv:6,integ:[0,5,7,1,3],collect:6,necessari:[3,7,5,2],either:[1,2,3,5,6,7],argumenterror:[7,6],output:[6,4,5,2],unnecessari:6,encount:[3,1,5],old:[6,2],often:5,"0x00b1f020":[],interact:1,some:[1,2,3,5,6,7],back:2,intern:5,mistak:1,proper:7,librari:7,absent:5,subpars:[7,2],avoid:5,normal:[1,2,5],definit:6,per:6,exit:[0,1,2,5,6,7],prog:[1,2,5,6,7,8],foo:[1,2,3,5,6,7],refer:[1,6,5],nargs_:[],object:[0,1,2,3,4,5,6,7,8],run:[0,3],inspect:[3,2],usag:[0,1,2,5,6,7,8],argument_default:[8,6],found:1,add_subpars:[7,2],"__name__":[0,3],"super":7,xyzz:1,about:[6,3,7,5,2],actual:7,callback_:7,optpars:[0,7],constructor:[7,6,2],commit:2,disabl:6,own:[6,7,5,2],xyz:[7,2],within:6,automat:[4,5,6],suppl:2,been:[3,7,5],strip:5,wrap:6,chang:[6,7,5,2],mark:5,yyi:[5,6],your:[6,3,7,5,2],manag:3,inclus:5,fill:[3,6],log:0,wai:[6,7,1,2,5],spam:[7,1,2],support:[6,7,1,2,5],"long":[7,1,6,5],custom:[6,8,1,2,5],avail:[6,5,2],start:[3,6],interfac:[0,7,5,3],includ:[6,7,1,2,5],lot:[7,2],parse_arg:[0,1,2,3,4,5,6,7,8],treat:[6,2],"function":[1,2,5],reduc:7,creation:[6,2],form:[3,7,5],tupl:[5,2],bufsiz:[4,5],msg:5,parents_:[],"0x00b8fe78":[],line:[0,1,2,3,4,5,6,7],conflict_handl:[8,6],concaten:1,made:1,input:5,temp:5,possibl:5,"default":[0,1,2,3,5,6,7,8],checkout:2,argumentdefaultshelpformatt:6,displai:[6,7,5,2],below:[7,5,6],otherwis:6,similar:7,gather:5,constant:5,creat:[0,1,2,3,4,5,6,7],"int":[0,1,2,3,5,6,7],descript:[0,2,3,5,6,8],parser:[0,1,2,3,4,5,6,7,8],"0x00b1f068":[],doesn:[7,5],strongli:2,exist:1,file:[0,2,3,4,5,6,7],xyzyx:2,simplest:1,probabl:5,again:7,readi:3,titl:2,when:[0,1,2,3,5,6,7],detail:[1,6,5],invalid:[8,7,1,5],other:[0,1,2,4,6,7,8],futur:2,scriptnam:0,varieti:1,test:7,set_default:[6,2],you:[1,2,3,5,6,7],bar_pars:[7,6],repeat:6,clean:6,why:7,consid:[5,6],"0x00adf020":[],svn:2,receiv:7,longer:[7,1,2],pseudo:[4,1],time:[7,5,2],backward:7},titles:["Documentation","The parse_args() method","Other methods","Introduction to argparse","Other utilities","The add_argument() method","ArgumentParser objects","argparse vs. optparse","API documentation"],modules:{},descrefs:{"":{parse_args:[1,0],parse_known_args:[2,0],add_mutually_exclusive_group:[2,0],set_defaults:[2,0],FileType:[4,1],add_argument:[5,0],ArgumentParser:[6,1],get_default:[2,0],add_subparsers:[2,0],add_argument_group:[2,0]}},filenames:["index","parse_args","other-methods","overview","other-utilities","add_argument","ArgumentParser","argparse-vs-optparse","api-docs"]}) \ No newline at end of file
+Search.setIndex({desctypes:{"0":"method","1":"class"},terms:{all:[6,3,7,5,2],code:[0,7,6],partial:[8,7,2],messg:7,whatev:2,illustr:[7,5],global:6,four:5,prefix:[1,6,5],subclass:2,follow:[0,7,5,6,3],formatter_class:[8,6],whose:[5,6],typeerror:[],"const":[8,3,7,1,5],fancier:2,readabl:4,prefix_char:[8,7,6],program:[6,0,5,2,3],present:[7,1,2,5],sens:[5,6],worth:5,consum:[7,5],everi:3,string:[1,2,3,5,6,7],fals:[6,7,5,2],subcommand:[7,2],util:[8,0,4],parent_pars:6,retriev:[],default_:[],exact:7,implement:5,level:2,list:[1,2,3,5,6,7],"try":[7,6],item:[5,2],adjust:6,stderr:2,pleas:6,subparser_nam:2,bacon:1,dogmat:7,past:7,zero:[7,5],fyoyo:7,group1:2,pass:[1,2,4,5,6,7],append:5,compat:7,what:[3,7,1,6,5],abc:[7,5],sub:[8,7,2],neg:1,section:[6,7,5,2],advanc:7,brief:[6,5,2],current:[6,2],abbrevi:[8,1],version:[8,5,6],"new":[7,1,6,5],method:[0,1,2,3,5,6,7,8],told:5,deriv:[7,5],gener:[0,7,5,6,3],never:[7,6],here:[7,1],let:2,inher:1,path:7,along:1,infil:[4,5],modifi:2,sinc:7,interpret:5,convert:[3,4,7,1,5],produc:[7,5,2],convers:5,pick:1,action:[1,2,3,5,6,7,8],weird:6,commonli:5,mutual:[8,2],control:6,regardless:6,extra:[1,2],appli:5,modul:[0,7,5,3],prefer:7,filenam:5,api:[8,0,7,5],txt:[0,5,6],select:[5,2],add_argument_group:2,from:[1,2,3,5,6,7],describ:[6,5,2],would:[7,5],frobbl:[5,2],regist:2,two:[6,3,1,2,5],few:[7,5,2],call:[0,1,2,3,5,6,7],add_help:[8,7,6,2],recommend:2,taken:[7,1,5],rawtexthelpformatt:6,type:[0,1,2,3,4,5,6,7,8],tell:[3,1],more:[0,1,3,5,6,7],sort:[3,5,6],parser_a:2,parser_b:2,notic:[7,1],warn:5,flag:[8,7,5],indic:[6,5,2],particular:[7,5,2],known:7,hold:[3,5],easiest:[7,5,2],must:[1,6,5],dictat:7,none:[4,7,1,6,5],word:[6,2],dest:[1,2,3,5,7,8],work:[6,2],conceptu:2,remain:[3,7,5,2],itself:5,can:[0,1,2,3,4,5,6,7],def:[7,5,2],overrid:[7,6,2],traceback:6,prompt:1,puriti:7,give:[1,6],frabbl:3,share:[7,6],add_argu:[0,1,2,3,4,5,6,7,8],accept:[7,5,2],cautiou:1,want:[6,3,7,5,2],alwai:[6,5,2],multipl:[7,5,2],thing:5,rather:[1,6],anoth:[6,5,2],check_method:7,fooaction:[7,5],write:[0,7,6,3],how:[6,3,1,2,5],sever:[6,1,2],instead:[7,5,2],narg:[0,1,3,5,6,7,8],simpl:[0,7,5,3],updat:2,parser_abc:7,foo_pars:[7,6],overridden:6,mess:6,max:[3,1],after:[6,7,1,2,5],variant:2,invok:[6,3,7,5,2],befor:[5,6],wrong:1,okai:6,mai:[6,7,1,2,5],end:[7,5,2],associ:[5,2],xfoox:7,"0x013a2410":[],"short":[1,6,5],attempt:[7,1],practic:7,stdin:[4,5],explicit:5,correspond:[3,7],ambigu:1,caus:[5,6],inform:[6,3,7,5,2],type_:[],maintain:[7,6],combin:[1,2],allow:[6,7,1,2,5],callabl:[7,5],order:2,origin:[7,6],least:[5,2],help:[0,1,2,3,5,6,7,8],over:[7,6],becaus:6,through:7,same:[7,5,6],hierarchi:7,metavar_:[],still:5,subparser2:2,subparser1:2,paramet:[7,5,6],perfect:5,style:1,interspers:7,group:[8,2],fit:6,better:[7,2],main:2,easier:1,them:[0,5,1,6,3],good:2,"return":[3,7,1,2,5],greater:6,thei:[7,1,6,5],python:[0,7,6,2],initi:[3,5],option_str:[7,5],now:[3,7],discuss:7,introduct:[0,3],choic:[1,2,3,5,7,8],allow_interspersed_arg:7,optionerror:7,name:[1,2,3,5,6,7,8],anyth:[7,5],choices_:[],separ:[1,2],easili:7,achiev:[1,6],mode:[4,7,5],each:[6,3,7,5,2],fulli:2,difficult:7,nbar:6,mean:[3,7,5,6],myprogram:6,replac:[7,6],idea:2,heavi:1,expect:[3,1,5],our:3,happen:7,beyond:[8,1],metavar:[0,1,3,5,7,8],special:[5,2],out:[0,4,5,6,3],variabl:7,accomplish:[1,6],referenc:6,space:[6,2],foo_bar:5,newli:1,parser_xyz:7,perfect_squar:5,print:[1,2,3,5,6,7,8],factori:[4,5],math:5,common:[3,5,6],optionvalueerror:7,worthwhil:7,situat:[1,5],given:6,argv:[8,3,1,6],standard:7,mmm:1,reason:7,base:7,dictionari:7,care:[7,2],indent:6,could:[1,5],omit:5,refus:7,keep:6,fromfile_prefix_char:[8,6,2],turn:3,place:6,isn:[7,5],retain:6,const_:[],assign:1,first:[5,6],oper:5,rang:[3,1],directli:5,carri:3,onc:[3,2],number:[6,7,1,2,5],restrict:5,instruct:3,alreadi:[7,1,6],construct:[7,6],wasn:5,open:[4,7,5,6],size:4,differ:[7,5,2],script:[3,2],top:2,sometim:[6,1,2],messag:[0,1,2,5,6,7],too:5,similarli:2,conveni:[7,5],"final":[],store:[3,5],monkei:7,option:[0,1,2,3,5,6,7,8],namespac:[1,2,3,4,5,6,7,8],copi:7,specifi:[0,1,2,3,5,6,7],pars:[0,1,2,3,5,7,8],store_tru:[3,7,1,2,5],exactli:[3,1,6],than:[6,7,1,2,5],format_usag:2,wide:6,kind:2,setattr:[7,5],whenev:[1,5],provid:[0,7,1,2,5],remov:6,charact:[7,1,6,5],str:5,were:[5,6],posit:[0,1,2,3,5,6,7],seri:[3,5],sai:7,abov:[6,0,1,2,3],"0x013a2380":[],group2:2,argument:[0,1,2,3,4,5,6,7,8],dash:7,add_pars:[7,2],manner:[3,7],have:[4,7,1,6,5],"__main__":[0,3],need:[6,7,5,2],seem:7,squar:5,equival:6,callback:7,self:[7,5,2],append_const:5,also:[6,1,2,5],builtin:5,exampl:[0,1,2,3,5,6],take:[1,2,3,5,6,7],which:[6,7,1,2,5],singl:[1,2,3,5,6,7],uppercas:5,begin:[3,1],sure:[7,5,2],though:5,buffer:4,previou:[7,1],most:[6,3,7,5,2],regular:2,pair:[7,6],choos:[7,5],"class":[4,7,1,6,5],appear:2,don:[7,1,5],later:3,request:[4,5,2],doe:[6,7,5,2],declar:7,determin:[1,2,3,5,6,7],occasion:6,sum:[0,1,3],"0x00b8fb18":[],parser_bar:2,show:[6,0,7,5,2],text:6,filetyp:[8,0,4,5],syntax:[8,7,1],particularli:[7,6,2],hack:7,find:[3,1,2],onli:[6,7,1,2,5],textual:6,parse_known_arg:2,just:[5,2],pretti:3,"true":[1,2,3,5,6,7],parser_foo:2,figur:5,should:[0,2,3,5,6,7],store_const:[3,7,1,5],wood:1,dict:5,"__call__":[7,5],add_opt:7,variou:[5,6],get:[6,3,7,1,2],express:6,report:5,requir:[8,7,1,2,5],bar:[1,2,3,5,6,7],keyword:[1,2,3,5,6,7],baz:[7,5,2],dramat:7,yield:2,patch:7,store_fals:[5,2],whether:[5,6],bad:[1,5],calcul:6,bac:1,contain:[1,2,5,6,7,8],where:[0,5,6],set:[6,3,7,5,2],"float":[7,5,2],see:[1,2,3,5,6,7],arg:[0,1,2,3,4,5,6,7],close:0,extend:[7,5],correctli:6,someth:3,written:0,subdir:6,between:6,"import":[0,7,5,6,3],awai:5,badger:[7,1,2],across:6,attribut:[1,2,3,5,6,7],altern:6,parent:[8,7,6,2],xrang:[3,7,1,5],disallow:[7,6],extens:7,outfil:5,come:7,addit:[6,7,5,2],both:[7,1],last:[1,6],howev:[6,7,5,2],against:7,etc:[7,1,2,5],eas:5,mani:[7,5],whole:[],simpli:[6,7,1,2,5],point:7,argumentpars:[0,1,2,3,4,5,6,7,8],dispatch:7,featur:7,suppli:[6,3,7,5,2],respect:5,assum:[0,5,2,3],duplic:7,quit:[7,5],coupl:[6,2],addition:5,three:6,empti:1,implicit:7,accumul:[3,1],secret:7,much:[3,7,1,2,5],valu:[1,2,5,6,7,8],basic:5,unambigu:1,print_usag:2,popul:[1,2],strategi:6,epilog:[8,6],suppress:[7,5,6],xxx:[7,5,6],great:5,ani:[6,7,5,2],upgrad:[0,7],understand:4,togeth:[1,5],func:2,child:6,repetit:5,those:[7,1],"case":[3,1,2,5],therefor:5,look:[3,7,1],format_help:2,formatt:6,defin:[6,5,2],"while":[7,1,6,5],behavior:[3,5,6],error:[6,7,1,2,5],argpars:[0,1,2,3,4,5,6,7],advantag:[0,7],stdout:[0,4,5],almost:[5,6],kwarg:[7,2],arg_lin:2,"__init__":7,clearli:1,perform:[5,2],make:[6,3,7,5,2],format:[6,7,5,2],sqrt:5,check:[7,1,2,5],handl:[7,5,2],complex:[7,5],help_:[],split:[1,2,3,5,6,7],document:[8,0,1,6],infer:5,dest_:[],complet:2,dedent:6,foo_bar_baz_pars:7,effect:2,rais:[5,6],user:[6,7,1,2,5],store_act:7,typic:[3,7,5,2],recent:6,appropri:[0,2,3,5,6,7],older:6,argumenttypeerror:5,thu:7,inherit:[7,6],likewis:6,without:2,command:[0,1,2,3,4,5,6,7,8],thi:[0,1,2,3,4,5,6,7],conflict:6,everyth:[7,1],sibl:2,usual:[6,7,5,2],identifi:5,execut:2,add_mutually_exclusive_group:2,note:[6,7,5,2],action_:[],exclus:[8,2],expos:7,had:7,except:[6,2],add:[0,2,3,5,6,7],valid:[7,5,2],remaining_arg:7,rawdescriptionhelpformatt:6,save:[0,3],match:[6,7,1,2,5],applic:[6,2],transpar:7,read:[6,5,2],textwrap:6,writabl:[4,5],built:[3,1],know:[7,5,2],print_help:[6,7,5,2],like:[1,2,3,5,6,7],insert:[1,6],get_default:2,specif:6,arbitrari:5,whitespac:6,manual:7,resolv:6,integ:[0,5,7,1,3],collect:6,necessari:[3,7,5,2],either:[1,2,3,5,6,7],argumenterror:[7,6],output:[6,4,5,2],unnecessari:6,encount:[3,1,5],old:[6,2],often:5,"0x00b1f020":[],interact:1,some:[1,2,3,5,6,7],back:2,intern:5,mistak:1,proper:7,librari:7,absent:5,subpars:[7,2],avoid:5,normal:[1,2,5],definit:6,per:[6,2],exit:[0,1,2,5,6,7],prog:[1,2,5,6,7,8],foo:[1,2,3,5,6,7],refer:[1,6,5],nargs_:[],object:[0,1,2,3,4,5,6,7,8],run:[0,3],inspect:[3,2],usag:[0,1,2,5,6,7,8],argument_default:[8,6],found:1,add_subpars:[7,2],"__name__":[0,3],"super":7,xyzz:1,about:[6,3,7,5,2],actual:7,meth:[],callback_:7,optpars:[0,7],constructor:[7,6,2],commit:2,disabl:6,own:[6,7,5,2],xyz:[7,2],within:6,automat:[4,5,6],suppl:2,been:[3,7,5],strip:[5,2],wrap:6,chang:[6,7,5,2],mark:5,yyi:[5,6],your:[6,3,7,5,2],manag:3,inclus:5,fill:[3,6],log:0,wai:[6,7,1,2,5],spam:[7,1,2],support:[6,7,1,2,5],"long":[7,1,6,5],custom:[6,8,1,2,5],avail:[6,5,2],start:[3,6],interfac:[0,7,5,3],includ:[6,7,1,2,5],lot:[7,2],parse_arg:[0,1,2,3,4,5,6,7,8],treat:[6,2],"function":[1,2,5],reduc:7,creation:[6,2],form:[3,7,5],tupl:[5,2],bufsiz:[4,5],continu:2,msg:5,parents_:[],"0x00b8fe78":[],line:[0,1,2,3,4,5,6,7],conflict_handl:[8,6],concaten:1,made:1,input:5,temp:5,possibl:5,"default":[0,1,2,3,5,6,7,8],checkout:2,argumentdefaultshelpformatt:6,displai:[6,7,5,2],below:[7,5,6],otherwis:6,similar:7,gather:5,constant:5,creat:[0,1,2,3,4,5,6,7],"int":[0,1,2,3,5,6,7],descript:[0,2,3,5,6,8],parser:[0,1,2,3,4,5,6,7,8],"0x00b1f068":[],doesn:[7,5],strongli:2,exist:1,file:[0,2,3,4,5,6,7,8],xyzyx:2,simplest:1,probabl:5,again:7,readi:3,titl:2,when:[0,1,2,3,5,6,7],detail:[1,6,5],invalid:[8,7,1,5],other:[0,1,2,4,6,7,8],futur:2,scriptnam:0,varieti:1,test:7,set_default:[6,2],you:[1,2,3,5,6,7],bar_pars:[7,6],repeat:6,clean:6,why:7,consid:[5,6],"0x00adf020":[],svn:2,receiv:7,longer:[7,1,2],pseudo:[4,1],time:[7,5,2],backward:7,convert_arg_line_to_arg:[6,2]},titles:["Documentation","The parse_args() method","Other methods","Introduction to argparse","Other utilities","The add_argument() method","ArgumentParser objects","argparse vs. optparse","API documentation"],modules:{},descrefs:{"":{parse_args:[1,0],parse_known_args:[2,0],add_mutually_exclusive_group:[2,0],set_defaults:[2,0],FileType:[4,1],add_argument:[5,0],ArgumentParser:[6,1],convert_arg_line_to_args:[2,0],get_default:[2,0],add_subparsers:[2,0],add_argument_group:[2,0]}},filenames:["index","parse_args","other-methods","overview","other-utilities","add_argument","ArgumentParser","argparse-vs-optparse","api-docs"]}) \ No newline at end of file
diff --git a/doc/source/ArgumentParser.rst b/doc/source/ArgumentParser.rst
index cbf62bd..f50ab5d 100644
--- a/doc/source/ArgumentParser.rst
+++ b/doc/source/ArgumentParser.rst
@@ -133,12 +133,11 @@ If the ``fromfile_prefix_chars=`` argument is given to the ArgumentParser constr
>>> parser.parse_args(['-f', 'foo', '@args.txt'])
Namespace(f='bar')
-Arguments read from a file must be one per line (with each whole line being considered a single argument) and are treated as if they were in the same place as the original file referencing argument on the command line.
+Arguments read from a file must by default be one per line (but see also :meth:`convert_arg_line_to_args`) and are treated as if they were in the same place as the original file referencing argument on the command line.
So in the example above, the expression ``['-f', 'foo', '@args.txt']`` is considered equivalent to the expression ``['-f', 'foo', '-f', 'bar']``.
The ``fromfile_prefix_chars=`` argument defaults to ``None``, meaning that arguments will never be treated as file references.
-
argument_default
----------------
diff --git a/doc/source/other-methods.rst b/doc/source/other-methods.rst
index 1e9ca1d..c54c654 100644
--- a/doc/source/other-methods.rst
+++ b/doc/source/other-methods.rst
@@ -277,3 +277,19 @@ Mutual exclusion
Note that currently mutually exclusive argument groups do not support the ``title`` and ``description`` arguments of :meth:`add_argument_group`. This may change in the future however, so you are *strongly* recommended to specify ``required`` as a keyword argument if you use it.
+Customizing file parsing
+------------------------
+
+.. method:: convert_arg_line_to_args(arg_line)
+
+ Arguments that are read from a file (see the ``fromfile_prefix_chars`` keyword argument to the :class:`ArgumentParser` constructor) are read one argument per line. If you need fancier parsing, then you can subclass the :class:`ArgumentParser` and override the :meth:`convert_arg_line_to_args` method.
+
+ This method takes a single argument ``arg_line`` which is a string read from the argument file. It returns a list of arguments parsed from this string. The method is called once per line read from the argument file, in order.
+
+ A useful override of this method is one that treats each space-separated word as an argument::
+
+ def convert_arg_line_to_args(self, arg_line):
+ for arg in arg_line.split():
+ if not arg.strip():
+ continue
+ yield arg
diff --git a/test/test_argparse.py b/test/test_argparse.py
index a7eb74a..2e78c88 100644
--- a/test/test_argparse.py
+++ b/test/test_argparse.py
@@ -175,6 +175,8 @@ class ParserTesterMetaclass(type):
# default parser signature is empty
if not hasattr(cls, 'parser_signature'):
cls.parser_signature = Sig()
+ if not hasattr(cls, 'parser_class'):
+ cls.parser_class = ErrorRaisingArgumentParser
# ---------------------------------------
# functions for adding optional arguments
@@ -238,7 +240,7 @@ class ParserTesterMetaclass(type):
def _get_parser(self, tester):
args = tester.parser_signature.args
kwargs = tester.parser_signature.kwargs
- parser = ErrorRaisingArgumentParser(*args, **kwargs)
+ parser = tester.parser_class(*args, **kwargs)
self._add_arguments(parser, tester.argument_signatures)
return parser
@@ -1325,6 +1327,36 @@ class TestArgumentsFromFile(TempDirMixin, ParserTestCase):
]
+class TestArgumentsFromFileConverter(TempDirMixin, ParserTestCase):
+ """Test reading arguments from a file"""
+
+ def setUp(self):
+ super(TestArgumentsFromFileConverter, self).setUp()
+ file_texts = [
+ ('hello', 'hello world!\n'),
+ ]
+ for path, text in file_texts:
+ file = open(path, 'w')
+ file.write(text)
+ file.close()
+
+ class FromFileConverterArgumentParser(ErrorRaisingArgumentParser):
+ def convert_arg_line_to_args(self, arg_line):
+ for arg in arg_line.split():
+ if not arg.strip():
+ continue
+ yield arg
+ parser_class = FromFileConverterArgumentParser
+ parser_signature = Sig(fromfile_prefix_chars='@')
+ argument_signatures = [
+ Sig('y', nargs='+'),
+ ]
+ failures = []
+ successes = [
+ ('@hello X', NS(y=['hello', 'world!', 'X'])),
+ ]
+
+
# =====================
# Type conversion tests
# =====================