Python argaprse optional arguments handling
<p>So, I have a python script for parsing and plotting data from text files. Argument handling is done with argparse module. The problem is, that some arguments are optional, e.g. one of the is used to add text annotations on the plot. This argument is sent to plotting function via **kwargs. My question is - what is the most pythonic way to handle those optional arguments? Some pseudo code here:</p>

<pre><code>parser = argparse.ArgumentParser()
...
parser.add_argument("-o", "--options", nargs="+", help="additional options")
args = parser.parse_args()
...
def some_function(arguments, **kwargs):
   doing something with kwargs['options']
   return something
...
arguments = ...
some_function(arguments, options=args.options)
</code></pre>

<p>If options are not specified by default None value is assigned. And it causes some problems. What is more pythonic - somehow check 'options' within some_function? Or maybe parse arguments before some_function is called?</p>

<p>use <code>get</code> and set a default value if the key is not found in the dict</p>

<pre><code>def some_function(arguments, **kwargs):
   something = kwargs.get('options', 'Not found')
   return something
</code></pre>

<p>or an if statement</p>

<pre><code>if 'option' in kwargs:
    pass # do something
</code></pre>

python command-line-arguments argparse kwargs