
    -z                        S r SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSKJ	r	  SSKJ
r
  \
R                  r " S S\5      rSqSqSqSqSqS	 rS
 rS'S jrS rS rS rS rS rS(S jrS r " S S\5      r " S S\5      rS'S jr S r!S r"  S)S jr# " S S\5      r$S r%S r&  S*S jr'S+S  jr(S! r)S" r*S# r+S$ r,  S,S% jr-\" 5         \.S&:X  a  \," 5         gg)-aY  This module is the base for programs that provide multiple commands.

This provides command line tools that have a few shared global flags,
followed by a command name, followed by command specific flags,
then by arguments. That is:
  tool [--global_flags] command [--command_flags] [args]

The module is built on top of app.py and 'overrides' a bit of it. However
the interface is mostly the same. The main difference is that your main
is supposed to register commands and return without further execution
of the commands; pre checking is of course welcome! Also your
global initialization should call appcommands.Run() rather than app.run().

To register commands use AddCmd() or AddCmdFunc().  AddCmd() is used
for commands that derive from class Cmd and the AddCmdFunc() is used
to wrap simple functions.

This module itself registers the command 'help' that allows users
to retrieve help for all or specific commands.  'help' is the default
command executed if no command is expressed, unless a different default
command is set with SetDefaultCommand.

Example:

<code>
from mx import DateTime


class CmdDate(appcommands.Cmd):
  \"\"\"This docstring contains the help for the date command.\"\"\"

  def Run(self, argv):
    print DateTime.now()


def main(argv):
  appcommands.AddCmd('date', CmdDate, command_aliases=['data_now'])


if __name__ == '__main__':
  appcommands.Run()
</code>

In the above example the name of the registered command on the command line is
'date'. Thus, to get the date you would execute:
  tool date
The above example also added the command alias 'data_now' which allows to
replace 'tool date' with 'tool data_now'.

To get a list of available commands run:
  tool help
For help with a specific command, you would execute:
  tool help date
For help on flags run one of the following:
  tool --help
Note that 'tool --help' gives you information on global flags, just like for
applications that do not use appcommand. Likewise 'tool --helpshort' and the
other help-flags from app.py are also available.

The above example also demonstrates that you only have to call
  appcommands.Run()
and register your commands in main() to initialize your program with appcommands
(and app).

Handling of flags:
  Flags can be registered just as with any other google tool using flags.py.
  In addition you can also provide command specific flags. To do so simply add
  flags registering code into the __init__ function of your Cmd classes passing
  parameter flag_values to any flags registering calls. These flags will get
  copied to the global flag list, so that once the command is detected they
  behave just like any other flag. That means these flags won't be available
  for other commands. Note that it is possible to register flags with more
  than one command.

  Commands can allow GNU-style scanning of their command-specific argv by
  calling appcommands.UseGnuGetOpt. This can be done within each command's
  __init__ function or globally in the program's main function. (Don't call
  FLAGS.set_gnu_getopt directly, because this will try to interpret command
  flags before they have been registered.)

Getting help:
  This module activates formatting and wrapping to help output. That is
  the main difference to help created from app.py. So just as with app.py,
  appcommands.py will create help from the main modules main __doc__.
  But it adds the new 'help' command that allows you to get a list of
  all available commands.  Each command's help will be followed by the
  registered command specific flags along with their defaults and help.
  After help for all commands there will also be a list of all registered
  global flags with their defaults and help.

  The text for the command's help can best be supplied by overwriting the
  __doc__ property of the Cmd classes for commands registered with AddCmd() or
  the __doc__ property of command functions registered AddCmdFunc().

Inner working:
  This module interacts with app.py by replacing its inner start dispatcher.
  The replacement version basically does the same, registering help flags,
  checking whether help flags were present, and calling the main module's main
  function. However unlike app.py, this module expects main() to only register
  commands and then to return. After having all commands registered
  appcommands.py will then parse the remaining arguments for any registered
  command. If one is found it will get executed. Otherwise a short usage info
  will be displayed.

  Each provided command must be an instance of Cmd. If commands get registered
  from global functions using AddCmdFunc() then the helper class _FunctionalCmd
  will be used in the registering process.

    )absolute_import)print_functionN)app)flagsc                       \ rS rSrSrSrg)AppCommandsError   z$The base class for all flags errors. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r
       -platform/bq/third_party/pyglib/appcommands.pyr   r      s    ,r   r   c                      Sq 0 q0 qSqSn U [        R
                  ;   a  [        R
                  U    S:H  qOSq[        S[        5        g)z9Reset the module for test purposes; ONLY use for testing.Nhelp)APPCOMMAND_USE_GNU_GET_OPT_FOR_SUBCOMMAND1F)		_cmd_argv	_cmd_list_cmd_alias_list_cmd_defaultosenviron_cmd_gnugetoptAddCmd_CmdHelp)use_gnu_opt_names    r   ResetForTestr!      sQ     ))/,@#ZZ 01S8N N 	r   c                  b    [         R                  R                  [        R                  S   5      $ )z2Returns the friendly basename of this application.r   )r   pathbasenamesysargvr
   r   r   GetAppBasenamer'      s    			#((1+	&&r   c                 
   [         R                  R                  5         U b"  [         R                  R	                  SU -  5        [         R                  R	                  S[        5       -  5        [         R                  " S5        g)zDisplay optional message, followed by a note on how to get help, then exit.

Args:
  message: optional message to display

Raises:
  SystemExit: always, to indicate exiting the program.
Nz%s
zRun '%s help' to get help
   )r%   stdoutflushstderrwriter'   exit)messages    r   ShortHelpAndExitr0      sW     **JJVg%&**0>3CCD((1+r   c                      [         $ )z#Return list of registered commands.)r   r
   r   r   GetCommandListr2      s
     
r   c                      [         $ )z*Return list of registered command aliases.)r   r
   r   r   GetCommandAliasListr4      s
     
r   c                      [        [        5       5      n [        5       R                  5        H  u  pU R	                  U5      X'   M     U $ )z6Return list of registered commands, including aliases.)dictr2   r4   itemsget)all_cmds	cmd_aliascmd_names      r   GetFullCommandListr<      s>    ."#(0288:i",,x0H ;	/r   c                 b    [        5       R                  [        5       R                  U 5      5      $ )zGet the command or None if name is not a registered command.

Args:
  name:  name of command to look for

Returns:
  Cmd instance holding the command or None
)r2   r8   r4   )names    r   GetCommandByNamer?      s'     
			1377=	>>r   c                      [         $ )zReturn list of remaining args.)r   r
   r   r   GetCommandArgvrA      s    	r   c                     U q g)z1Allow mixed flag/arg ordering in subcommand argv.N)r   )choices    r   UseGnuGetOptrD      s	     .r   c                  f    [        [        5        V s/ s H  n [        U 5      PM     sn 5      $ s  sn f )z5Returns the length of the longest registered command.)maxr2   len)r;   s    r   GetMaxCommandLengthrH      s(    	N,<=,<c(m,<=	>>=s   .c                   H    \ rS rSrSr  SS jrS rS rSS jrS r	S	 r
S
rg)Cmd   aG  Abstract class describing and implementing a command.

When creating code for a command, at least you have to derive this class
and override method Run(). The other methods of this class might be
overridden as well. Check their documentation for details. If the command
needs any specific flags, use __init__ for registration.
Nc                     Xl         X0l        X l        X@l        XPl        X`l        [        U 5      [        L a  [        S5      eg)a  Initialize and check whether self is actually a Cmd instance.

This can be used to register command specific flags. If you do so
remember that you have to provide the 'flag_values=flag_values'
parameter to any flags.DEFINE_*() call.

Args:
  name:              Name of the command
  flag_values:       FlagValues() instance that needs to be passed as
                     flag_values parameter to any flags registering call.
  command_aliases:   A list of aliases that the command can be run as.
  all_commands_help: A short description of the command that is shown when
                     the user requests help for all commands at once.
  help_full:         A long description of the command and usage that is
                     shown when the user requests help for just this
                     command. If unspecified, the command's docstring is
                     used instead.
  hidden:            Whether to hide this command from the help output.

Raises:
  AppCommandsError: if self is Cmd (Cmd is abstract)
z*Cmd is abstract and cannot be instantiatedN)	_command_name_command_aliases_command_flags_all_commands_help
_help_full_hiddentyperJ   r   )selfr>   flag_valuescommand_aliasesall_commands_help	help_fullhiddens          r   __init__Cmd.__init__   sC    0 +%/OLDzSIJJ r   c                 t    [        [        U 5      R                  < S[        U 5      R                  < S35      e)a   Execute the command. Must be provided by the implementing class.

Args:
  unused_argv: Remaining command line arguments after parsing flags and
               command (in other words, a copy of sys.argv at the time of
               the function call with all parsed flags removed).

Returns:
  0 for success, anything else for failure (must return with integer).
  Alternatively you may return None (or not use a return statement at all).

Raises:
  AppCommandsError: Always, as this method must be overridden.
.z.Run() is not implemented)r   rS   r   r   )rT   unused_argvs     r   RunCmd.Run  s/     T
tDz224 5 5r   c                 D  ^  [         R                  T R                  5        [        R                  n  SU 4S jjnU[        l          [        U5      nT R                  R                  5         T R                  U5      nUc  SnO[        U[        5      (       d   eUU[        l        [         R                  T R                  5        $ ! [        R                   a)  n[        R                  " SXUR                  S9   SnAOHSnAf  [         R                  (       a*  [        R                  " 5         [         R"                  " 5         e = f U[        l        [         R                  T R                  5        g! U[        l        [         R                  T R                  5        f = f)aK  Execute the command with given arguments.

First register and parse additional flags. Then run the command.

Returns:
  Command return value.

Args:
  argv: Remaining command line arguments after parsing command and flags
        (that is a copy of sys.argv at the time of the function call with
        all parsed flags removed).
r   r)   Nc           	      4   > [        XUUTR                  SS9  g NT)exitcodeshow_cmdshow_global_flags)AppcommandsUsagerM   )	shorthelpwriteto_stdoutdetailed_errorrd   rT   s       r   ReplacementAppUsage+Cmd.CommandRun.<locals>.ReplacementAppUsage?  s    y. (43E3E)-/r   )rh   rj   rd   )r   r)   NN)FLAGSappend_flag_valuesrO   r   usageParseFlagsWithUsagemark_as_parsedr_   
isinstanceintremove_flag_values
UsageErrorrd   pdb_post_mortem	traceback	print_excpdbpost_mortem)rT   r&   orig_app_usagerk   reterrors   `     r   
CommandRunCmd.CommandRun-  s3    
T001YYNJN%)/
 $CI4"4(**,hhtn;#C%%
%% !cit223 ^^ N		AennM  




//
 	N !cit223 !cit223s,   AC EC?:E3 ?AEE3 3,Fc                 b   [        U[        5      (       a<  [        U5      S:  a-  U R                  b   [        R
                  " U R                  5      $ U R                  b   [        R
                  " U R                  5      $ U R                  (       a   [        R
                  " U R                  5      $ g)a  Get help string for command.

Args:
  unused_argv: Remaining command line flags and arguments after parsing
               command (that is a copy of sys.argv at the time of the
               function call with all parsed flags removed); unused in this
               default implementation, but may be used in subclasses.
  cmd_names:   Complete list of commands for which help is being shown at
               the same time. This is used to determine whether to return
               _all_commands_help, or the command's docstring.
               (_all_commands_help is used, if not None, when help is being
               shown for more than one command, otherwise the command's
               docstring is used.)

Returns:
  Help string, one of the following (by order):
    - Result of the registered 'help' function (if any)
    - Doc string of the Cmd class (if any)
    - Default fallback string
r)   zNo help available)rr   listrG   rP   r   doc_to_helprQ   r   )rT   r^   	cmd_namess      r   CommandGetHelpCmd.CommandGetHelp]  s}    * 	9d##I(:+t6677		$t//	t||,, r   c                     U R                   $ )zOGet aliases for command.

Returns:
  aliases: list of aliases for the command.
)rN   rT   s    r   CommandGetAliasesCmd.CommandGetAliases|  s        r   c                     U R                   $ )z/Get name of command.

Returns:
  Command name.
)rM   r   s    r   CommandGetNameCmd.CommandGetName  s     r   )rP   rN   rO   rM   rQ   rR   )NNNFN)r   r   r   r   r   rZ   r_   r~   r   r   r   r   r
   r   r   rJ   rJ      s0     9=>CKB5$.4`!>!r   rJ   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )_FunctionalCmdi  zPClass to wrap functions as CMD instances.

Args:
  cmd_func:   command function
c                 f   > SU;  a  UR                   US'   [        [        U ]  " X40 UD6  X0l        g)an  Create a functional command.

Args:
  name:        Name of command
  flag_values: FlagValues() instance that needs to be passed as flag_values
               parameter to any flags registering call.
  cmd_func:    Function to call when command is to be executed.
  **kargs:     Additional keyword arguments to be passed to the
               superclass constructor.
rX   N)r   superr   rZ   	_cmd_func)rT   r>   rU   cmd_funckargs	__class__s        r   rZ   _FunctionalCmd.__init__  s6     %#++eK	.$(DeDNr   c                 $    U R                  U5      $ )a	  Execute the command with given arguments.

Args:
  argv: Remaining command line flags and arguments after parsing command
        (that is a copy of sys.argv at the time of the function call with
        all parsed flags removed).

Returns:
  Command return value.
r   )rT   r&   s     r   r_   _FunctionalCmd.Run  s     >>$r   r   	r   r   r   r   r   rZ   r_   r   __classcell__r   s   @r   r   r     s        r   r   c                     [        UR                  [        5      (       d  [        S5      eU /U=(       d    / -    H  n[	        U5        U [
        U'   M     U[        U '   g)a:  Add a command from a Cmd instance.

Args:
  command_name:    name of the command which will be used in argument parsing
  cmd:             Cmd instance to register
  command_aliases: A list of command aliases that the command can be run as.

Raises:
  AppCommandsError: If cmd is not a subclass of Cmd.
  AppCommandsError: If name is already registered OR name is not a string OR
                    name is too short OR name does not start with a letter OR
                    name contains any non alphanumeric characters besides
                    '_', '-', or ':'.
+Command must be an instance of commands.CmdN)
issubclassr   rJ   r   _CheckCmdNamer   r   )command_namecmdrV   _r>   s        r   _AddCmdInstancer     sU    & 
CMM3	'	'
H
IIn 526d$(OD 7  )Lr   c                    U [        5       ;   a  [        SU -  5      e[        U [        5      (       a  [	        U 5      S::  a  [        S[        U 5      -  5      eU S   R                  5       (       d  [        SU -  5      eU  Vs/ s H#  oR                  5       (       a  M  US;   a  M!  UPM%     sn(       a  [        SU -  5      egs  snf )	a  Only allow strings for command names and aliases (reject unicode as well).

Args:
  name_or_alias: properly formatted string name or alias.

Raises:
  AppCommandsError: If name is already registered OR name is not a string OR
                    name is too short OR name does not start with a letter OR
                    name contains any non alphanumeric characters besides
                    '_', '-', or ':'.
z%Command or Alias '%s' already definedr)   z&Command '%s' not a string or too shortr   z)Command '%s' does not start with a letter)r   -:z1Command '%s' contains non alphanumeric charactersN)r4   r   rr   strrG   isalphaisalnum)name_or_aliascs     r   r   r     s     )++
B() * *	M3	'	'3}+=+B
C /0 1 1	q		!	!	#	#
F*+ , ,LAyy{aa?6JaL
N*+ , , MLs   CC&Cc                     U" U [         R                  " 5       40 UD6n[        U[        5      (       d  [	        S5      e[        X40 UD6  g)aU  Add a command from a Cmd subclass or factory.

Args:
  command_name:    name of the command which will be used in argument parsing
  cmd_factory:     A callable whose arguments match those of Cmd.__init__ and
                   returns a Cmd. In the simplest case this is just a subclass
                   of Cmd.
  **kwargs:        Additional keyword arguments to be passed to the
                   cmd_factory at initialization. Also passed to
                   _AddCmdInstance to catch command_aliases.

Raises:
  AppCommandsError: if calling cmd_factory does not return an instance of Cmd.
r   N)r   
FlagValuesrr   rJ   r   r   )r   cmd_factorykwargsr   s       r   r   r     sD     	L%"2"2"4??#	C		
H
II,.v.r   c                 X    [        U [        U [        R                  " 5       UUUUS9US9  g)a  Add a new command to the list of registered commands.

Args:
  command_name:      name of the command which will be used in argument
                     parsing
  cmd_func:          command function, this function received the remaining
                     arguments as its only parameter. It is supposed to do the
                     command work and then return with the command result that
                     is being used as the shell exit code.
  command_aliases:   A list of command aliases that the command can be run as.
  all_commands_help: Help message to be displayed in place of func.__doc__
                     when all commands are displayed.
  hidden:            Whether to hide this command from the help output.
)rV   rW   rY   )rV   N)r   r   r   r   )r   r   rV   rW   rY   s        r   
AddCmdFuncr      s3      , u/?/?/A81@3D(.0 #23r   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )r   i  zMStandard help command.

Allows to provide help for all or specific commands.
c                 ^   > SU;  a  SS[        5       0-  US'   [        [        U ]  " U0 UD6  g )NrX   zHelp for all or selected command:
	%(prog)s help [<command>]

To retrieve help with global flags:
	%(prog)s --help

To retrieve help with flags only from the main module:
	%(prog)s --helpshort [<command>]

prog)r'   r   r   rZ   )rT   argsr   r   s      r   rZ   _CmdHelp.__init__  sB    & 3 ^%&' [ 
(D"D3F3r   c           	      p    [        U5      S:  a  US   [        5       ;   a  US   nOSn[        SSSSUSS9  g)a  Execute help command.

If an argument is given and that argument is a registered command
name, then help specific to that command is being displayed.
If the command is unknown then a fatal error will be displayed. If
no argument is present then help for all commands will be presented.

If a specific command help is being generated, the list of commands is
temporarily replaced with one containing only that command. Thus the call
to usage() will only show help for that command. Otherwise call usage()
will show help for all registered commands as it sees all commands.

Args:
  argv: Remaining command line flags and arguments after parsing command
        (that is a copy of sys.argv at the time of the function call with
        all parsed flags removed).
        So argv[0] is the program and argv[1] will be the first argument to
        the call. For instance 'tool.py help command' will result in argv
        containing ('tool.py', 'command'). In this case the list of
        commands is searched for 'command'.

Returns:
  1 for failure
r)   Nr   F)rh   ri   rj   rd   re   rf   )rG   r<   rg   )rT   r&   re   s      r   r_   _CmdHelp.Run*  sB    2 4y1}a$6$88ahhq4(eMr   r
   r   r   s   @r   r   r     s    

4M Mr   r   c                      S[        5       -  $ )zKGet synopsis for program.

Returns:
  Synopsis including program basename.
z6%s [--global_flags] <command> [--command_flags] [args])r'   r
   r   r   GetSynopsisr   K  s     
B
 r   c                 p   / nU(       a  [        U5      S:X  a  UR                  S[        5       -  5        U(       a   [        U5      [        [        5       5      :X  a  UR                  S[        5       -  5        U b,  U(       a  UR                  S5        UR                  SU -  5        SR	                  U5      $ )a  Output a footer at the end of usage or help output.

Args:
  detailed_error: additional detail about why usage info was presented.
  cmd_names:      list of command names for which help was shown or None.
Returns:
  Generated footer that contains 'Run..' messages if appropriate.
r)   z4Run '%s help' to see the list of available commands.z2Run '%s help <command>' to get help for <command>. %s
)rG   appendr'   r2   join)rj   r   footers      r   _UsageFooterr   U  s     &	c)n)
MMH"$% &	c)nN,<(==
MMF"$% &mmB
MM$'(	6	r   c           	      
   U(       a  [         R                  nO[         R                  nSR                  [	        5       S-   5      n[         R
                  S   R                  nU(       ax  [        R                  " UR                  S[        5       5      5      n	UR                  [        R                  " U	[        R                  " 5       5      5        UR                  S5        U(       a  UR                  S5      S:X  aY  S[        5       -   n
UR                  [        R                  " U
[        R                  " 5       SS5      5        UR                  S5        [!        [#        5       5      S	:X  a  / nO[#        5       R%                  5        VVs/ s H  u  pUR&                  (       a  M  UPM     nnnUR)                  5         Ub  US:X  am  UR                  S5        SR+                  U5      nUR                  [        R                  " U[        R                  " 5       S5      5        UR                  S5        Ub  U/nO3[,        R.                  (       d  [,        R0                  (       d  U (       a  / nU GH  n[3        U5      n UR5                  [7        5       US9nUR=                  5       nSR+                  UR?                  5       /URA                  5       =(       d    / -   5      n[!        U5      S	-   [!        U5      :  d  U(       dM  UR                  [        R                  " U[        R                  " 5       5      5        UR                  S5        UnOURC                  [	        5       S-   5      nU(       aM  UR                  [        R                  " U[        R                  " 5       UU5      5        UR                  S5        OUR                  S5        [!        U5      S	:X  d  GMt  URD                  nU(       d  GM  UR                  U< SU< S35        UR                  URG                  US-   5      5        UR                  S5        GM     UR                  S5        U(       aq  UR                  S5        U (       a$  UR                  [,        RI                  5       5        O#UR                  [,        RG                  5       5        UR                  S5        OUR                  S[        5       -  5        UR                  S[K        X+5      -  5        Ub  [         RL                  " U5        g
g
s  snnf ! [8         a  nSU< S[;        U5      < S3n S
nAGNS
nAff = f)a<  Output usage or help information.

Extracts the __doc__ string from the __main__ module and writes it to
stderr. If that string contains a '%s' then that is replaced by the command
pathname. Otherwise a default usage string is being generated.

The output varies depending on the following:
- FLAGS.help
- FLAGS.helpshort
- show_cmd
- show_global_flags

Args:
  shorthelp:      print only command and main module flags, rather than all.
  writeto_stdout: write help message to stdout, rather than to stderr.
  detailed_error: additional details about why usage info was presented.
  exitcode:       if set, exit with this status code after writing help.
  show_cmd:       show help for this command only (name of command).
  show_global_flags: show help for global flags.

Raises:
  SystemExit: to indicate exiting the program.
r      __main__r   z


zUSAGE: z       r)   Nr   zAny of the following commands:
z, z  )r   zInternal error for command 'z': r]   r   z

z
Flags for z:
zGlobal flags:
z-Run '%s --help' to get help for global flags.z
%s
)'r%   r*   r,   rjustrH   modulesr   r   r   replacer'   r-   	text_wrapget_help_widthfindr   rG   r2   r7   rR   sortr   rm   r   	helpshortr?   r   rA   	Exceptionr   stripr   r   ljustrO   get_helpmain_module_helpr   r.   )rh   ri   rj   rd   re   rf   stdfileprefixdochelp_msgsynopsisr   r;   r   r>   commandcmd_helpr}   	all_namesprefix1	cmd_flagss                        r   rg   rg   l  s   2 jjGjjG88')A-.&J''#  T>3C!DEHMM%//(E,@,@,BCDMM(	"$;=(HMM%//(E,@,@,BI"$& 'MM(	aI '5&6&<&<&>%&>]X &>  % NN8v-mm67IIi cmmEOOC)=)=)?FGmmH*i	u)idt$GM''(8I'Nh ~~H						!"g&?&?&A&GRHJI
9~S[( mmEOOIu/C/C/EFGmmDg 3 5 9:gmmEOOHe.B.B.Df$+- .mmFmmD
9~ ((i	VT:;i((56fA B 
--MM#$mmE**,-mmENN$%MM$MMA"$% &	--<BBCHHX }%(  M;?ULhMs$   T*&T*T00
U:UUc                     [         R                  5       nU(       a  [         R                  S5        [        U 5      q[         R                  U5        [        $ ! [        R
                   a  n[        SU-  5         SnAgSnAff = f)zParse the flags, exiting (after printing usage) if they are unparseable.

Args:
  argv: List of command line arguments.
  force_gnu_off: If True, force GNU-style off for flags parsing.

Returns:
  remaining command line arguments after parsing flags
FzFATAL Flags parsing error: %sN)rm   is_gnu_getoptset_gnu_getoptr   r   Errorr0   )r&   force_gnu_offold_use_gnu_get_optr}   s       r   rp   rp     sj    >--/5!dI	,-	 >4u<==>s   AA B,A??Bc                     [        [        5      q[        [        5      S:  a  U (       a  [        S5        g[	        [        S   5      nUc  [        S[        S   -  5        [        S	 U$ )ab  Get the command or return None (or issue an error) if there is none.

Args:
  command_required: whether to issue an error if no command is present

Returns:
  command or None, if command_required is True then return value is a valid
  command or the program will exit. The program also exits if a command was
  specified but that command does not exist.
r   z%FATAL Command expected but none givenNr)   FATAL Command '%s' unknown)rp   r   rG   r0   r?   )command_requiredr   s     r   
GetCommandr     sZ     "),)^a>?Yq\*'_1IaL@Al	.r   c                     U q g)zChange the default command to execute if none is explicitly given.

Args:
  default_command: str, the name of the command to execute by default.
N)r   )default_commands    r   SetDefaultCommandr     s	     !,r   c                 <    [         R                  S   R                  [        5       5        [        [        5       5      S:  a
  [        SS9nO$[        [        5      nUc  [        S[        -  5        [        R!                  ["        5        [         R
                  " UR%                  [        5       5      5      e! [         a%  n[         R
                  " UR                  5      eSnAf[         a-  n[        R                  " 5         [        SU-  5         SnANSnAff = f)ao  Main initialization.

Calls __main__.main(), and then the command indicated by the first
non-flag argument, or 'help' if no argument was given.  (The command
to execute if no flag is given can be changed via SetDefaultCommand).

Only non-flag arguments are passed to main(). If main does not call
sys.exit, the return value of the command is used as the exit status.
r   Nz
FATAL error in main: %sr)   T)r   r   )r%   r   mainrA   
SystemExitr.   coder   rw   rx   r0   rG   r   r?   r   rm   r   r   r~   )r^   er}   r   s       r   _CommandsStartr     s    :KK
  !12 		Q$/G|,G3lBC~&##N$4566 
 
((166
	 :05899:s#   *B7 7
D C!!D.#DDc                      [         R                  S5        [        R                  n [        [        l         [        R
                  " [        [        S9U [        l        $ ! U [        l        f = f)zThis must be called from __main__ modules main, instead of app.run().

app.run will base its actions on its stacktrace.

Returns:
  app.run()
F)flags_parser)rm   r   r   ro   _ReplacementAppUsagerunr   rp   )original_usages    r   r_   r_   8  sE     u99."#)77>0CDCICIs   A A+c           	          [        XX#S SS9  g rc   )rg   )rh   ri   rj   rd   s       r   r   r   J  s    9n D:r   r   r   )T)NNF)r   r   NNNF)F)r   r   NN)/r   
__future__r   r   r   ry   r%   rw   abslr   r   rm   r   r   r   r   r   r   r   r!   r'   r0   r2   r4   r<   r?   rA   rD   rH   objectrJ   r   r   r   r   r   r   r   r   rg   rp   r   r   r   r_   r   r   r
   r   r   <module>r      s+  l\ ' % 	 
 
   y 
 		$'
 	?
?
Y& Yx" S " J :,4/. 8<.3300Ms 0Mf. DHEJm`>04!7>$ HL"&: z% r   