class_docstr: Simple class for commandline arguments parsing.
  
  `CmdlineParser` provides a simple interface to interpret commandline arguments
  and create new commandlines. This class exposes an interface to transform a list of strings
  representing the commandline in a dictionary of key-value conserving the order of
  the arguments, the value of named arguments, be it boolean, single-valued or multiple ones,
  as well as handling the definition of argument through the `=` sign.
  
  Attributes
  ----------
  arguments : dict
      Commandline arguments' name and value(s).
  template : list
      List of template-ready strings for formatting.
  
  See Also
  --------
  parse : Parse the list of string and defines the form of the template.
  
  Notes
  -----
  Subcommands are not supported and will be likely lead to bad interpretation of the commandline.
  Aggregation of single characters arguments is not supported yet. Ex: `-xzvf`
docstr: Parse the `commandline` argument.
  
  This function will go through the list of strings and create a dictionary
  mapping every argument to its value(s). It will also form a template to recreate such a
  list. If `commandline` is empty or the `parse` function has already been called, it
  will return the dictionary of the already-parsed arguments.
  
  Parameters
  ----------
  commandline : list
      List of string representing the commandline arguments.
  
  Returns
  -------
  dict
      Dictionary holding the values of every argument. The keys are the name of the arguments.
  
  Raises
  ------
  ValueError
      This exception is raised then the parser detects a duplicate argument.
  RuntimeError
      This exception is raised if the parser already parsed a commandline and contains a
      template.
  
  Notes
  -----
  Each argument is stored inside a `dict`.
  Inside that dictionary, the keys are created following these rules:
  
  -If the argument is a positional one, its key will be `'_pos_x'` where `x` is its index
  inside the list.
  -Otherwise, the key will be equal to the name of the argument minus the prefixed dashes.
  
  The values are stored in the following ways:
  
  -If the argument is a positional one, its value is simply its value inside the commandline.
  -If the argument is a named, boolean argument, its value is simply `True`.
  -If the argument is a named, single-valued argument, its value is stored as-is.
  -If the argument is a named, multi-valued argument, its value is a list containing each
  value following it until the next named argument.
  
  Positional arguments following a named argument are not currently supported.
  
  Examples
  --------
  Positional and named arguments:
  
  >>> parser = CmdlineParser()
  >>> parser.parse('python 1 --arg value'.split(' '))
  {'_pos_0': 'python', '_pos_1': '1', 'arg': 'value'}
  
  Named boolean argument:
  
  >>> parser.parse('python --boolean'.split(' '))
  {'_pos_0': 'python', 'boolean': True}
  
  Named multi-valued argument:
  
  >>> parser.parse('python --args value1 value2'.split(' '))
  {'_pos_0': 'python', 'args': ['value1', 'value2']}
  
  Named argument defined with `=`:
  
  >>> parser.parse('python --arg=value'.split(' '))
  {'_pos_0': 'python', 'arg': 'value'}
docstr_tok: ["Parse","the","`","commandline","`","argument",".","This","function","will","go","through","the","list","of","strings","and","create","a","dictionary","mapping","every","argument","to","its","value","(","s",")",".","It","will","also","form","a","template","to","recreate","such","a","list",".","If","`","commandline","`","is","empty","or","the","`","parse","`","function","has","already","been","called",",","it","will","return","the","dictionary","of","the","already-parsed","arguments",".","Parameters","--","--","--","--","--","commandline",":","list","List","of","string","representing","the","commandline","arguments",".","Returns","--","--","--","-","dict","Dictionary","holding","the","values","of","every","argument",".","The","keys","are","the","name","of","the","arguments",".","Raises","--","--","--","ValueError","This","exception","is","raised","then","the","parser","detects","a","duplicate","argument",".","RuntimeError","This","exception","is","raised","if","the","parser","already","parsed","a","commandline","and","contains","a","template",".","Notes","--","--","-","Each","argument","is","stored","inside","a","`","dict","`",".","Inside","that","dictionary",",","the","keys","are","created","following","these","rules",":","-If","the","argument","is","a","positional","one",",","its","key","will","be","`","'_pos_x","'","`","where","`","x","`","is","its","index","inside","the","list",".","-Otherwise",",","the","key","will","be","equal","to","the","name","of","the","argument","minus","the","prefixed","dashes",".","The","values","are","stored","in","the","following","ways",":","-If","the","argument","is","a","positional","one",",","its","value","is","simply","its","value","inside","the","commandline",".","-If","the","argument","is","a","named",",","boolean","argument",",","its","value","is","simply","`","True","`",".","-If","the","argument","is","a","named",",","single-valued","argument",",","its","value","is","stored","as-is",".","-If","the","argument","is","a","named",",","multi-valued","argument",",","its","value","is","a","list","containing","each","value","following","it","until","the","next","named","argument",".","Positional","arguments","following","a","named","argument","are","not","currently","supported",".","Examples","--","--","--","--","Positional","and","named","arguments",":",">",">",">","parser","=","CmdlineParser","(",")",">",">",">","parser.parse","(","'python","1","--","arg","value'.split","(","'","'",")",")","{","'_pos_0","'",":","'python","'",",","'_pos_1","'",":","'","1","'",",","'arg","'",":","'value","'","}","Named","boolean","argument",":",">",">",">","parser.parse","(","'python","--","boolean'.split","(","'","'",")",")","{","'_pos_0","'",":","'python","'",",","'boolean","'",":","True","}","Named","multi-valued","argument",":",">",">",">","parser.parse","(","'python","--","args","value1","value2'.split","(","'","'",")",")","{","'_pos_0","'",":","'python","'",",","'args","'",":","[","'value1","'",",","'value2","'","]","}","Named","argument","defined","with","`","=","`",":",">",">",">","parser.parse","(","'python","--","arg=value'.split","(","'","'",")",")","{","'_pos_0","'",":","'python","'",",","'arg","'",":","'value","'","}"]
code: def parse(self, commandline):
      """Parse the `commandline` argument.
  
      This function will go through the list of strings and create a dictionary
      mapping every argument to its value(s). It will also form a template to recreate such a
      list. If `commandline` is empty or the `parse` function has already been called, it
      will return the dictionary of the already-parsed arguments.
  
      Parameters
      ----------
      commandline : list
          List of string representing the commandline arguments.
  
      Returns
      -------
      dict
          Dictionary holding the values of every argument. The keys are the name of the arguments.
  
      Raises
      ------
      ValueError
          This exception is raised then the parser detects a duplicate argument.
      RuntimeError
          This exception is raised if the parser already parsed a commandline and contains a
          template.
  
      Notes
      -----
      Each argument is stored inside a `dict`.
      Inside that dictionary, the keys are created following these rules:
  
      -If the argument is a positional one, its key will be `'_pos_x'` where `x` is its index
      inside the list.
      -Otherwise, the key will be equal to the name of the argument minus the prefixed dashes.
  
      The values are stored in the following ways:
  
      -If the argument is a positional one, its value is simply its value inside the commandline.
      -If the argument is a named, boolean argument, its value is simply `True`.
      -If the argument is a named, single-valued argument, its value is stored as-is.
      -If the argument is a named, multi-valued argument, its value is a list containing each
      value following it until the next named argument.
  
      Positional arguments following a named argument are not currently supported.
  
      Examples
      --------
      Positional and named arguments:
  
      >>> parser = CmdlineParser()
      >>> parser.parse('python 1 --arg value'.split(' '))
      {'_pos_0': 'python', '_pos_1': '1', 'arg': 'value'}
  
      Named boolean argument:
  
      >>> parser.parse('python --boolean'.split(' '))
      {'_pos_0': 'python', 'boolean': True}
  
      Named multi-valued argument:
  
      >>> parser.parse('python --args value1 value2'.split(' '))
      {'_pos_0': 'python', 'args': ['value1', 'value2']}
  
      Named argument defined with `=`:
  
      >>> parser.parse('python --arg=value'.split(' '))
      {'_pos_0': 'python', 'arg': 'value'}
  
      """
      if self._already_parsed:
          raise RuntimeError("The commandline has already been parsed.")
  
      keys, arguments = self._parse_arguments(commandline)
      self.arguments = arguments
      self.keys = keys
  
      for key, value in arguments.items():
          # Handle positional arguments
          if key.startswith("_"):
              self.template.append("{" + key + "}")
  
          # Handle optional ones
          else:
              arg = self.keys[key]
              if arg in self.template:
                  continue
  
              self.template.append(arg)
  
              # Ignore value as key is a boolean argument
              if isinstance(value, bool):
                  continue
  
              if not isinstance(value, list):
                  template = "{" + key + "}"
                  self.template.append(template)
                  continue
  
              for pos in range(len(value)):
                  template = "{" + key + "[" + str(pos) + "]}"
                  self.template.append(template)
  
      self._already_parsed = True
  
      return self.arguments
  
file_path: orion/src/orion/core/io/cmdline_parser.py
imports: import os
  from collections import OrderedDict
label_desc: Use image node_id 5 for calling the CmdlineParser obj's underlying member method code with example usage: obj.parse(commandline) and returns: self