Belle II Software  release-05-02-19
utilities.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import sys
5 import argparse
6 
7 
8 class DefaultHelpArgumentParser(argparse.ArgumentParser):
9 
10  """An argparse.Argument parse slightly changed such
11  that it always prints an extended help message incase of a parsing error."""
12 
13  def error(self, message):
14  """Method invoked when a parsing error occured.
15  Writes an extended help over the base ArgumentParser.
16  """
17  self.print_help()
18  sys.stderr.write('error: %s\n' % message)
19  sys.exit(2)
20 
21 
22 class NonstrictChoices(list):
23 
24  """Class that instances can be given to an argparse.ArgumentParser.add_argument as choices keyword argument.
25 
26  The explicit choices stated during construction of this object are just suggestions but all other values are
27  excepted as well.
28  """
29 
30  def __contains__(self, value):
31  """Test for correctness of the choices.
32  Always returns true since all choices should be valid not only the ones stated at construction of this object.
33  """
34  return True
35 
36  def __iter__(self):
37  """Displays all explicit values and a final "..." to indicate more choices might be possible."""
38  # Append an ellipses to indicate that there are more choices.
39  copy = list(super(NonstrictChoices, self).__iter__())
40  copy.append('...')
41  return iter(copy)
42 
43  def __str__(self):
44  """Displays all explicit values and a final "..." to indicate more choices might be possible."""
45  # Append an ellipses to indicate that there are more choices.
46  copy = list(self)
47  copy.append('...')
48  return str(copy)
tracking.utilities.NonstrictChoices
Definition: utilities.py:22
tracking.utilities.DefaultHelpArgumentParser
Definition: utilities.py:8
tracking.utilities.NonstrictChoices.__str__
def __str__(self)
Definition: utilities.py:43
tracking.utilities.NonstrictChoices.__contains__
def __contains__(self, value)
Definition: utilities.py:30
tracking.utilities.NonstrictChoices.__iter__
def __iter__(self)
Definition: utilities.py:36
tracking.utilities.DefaultHelpArgumentParser.error
def error(self, message)
Definition: utilities.py:13