
    M/eX                     t   d Z ddlZddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ ddlZdd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z# ddl$m%Z% ddl&m'Z' ddl&m(Z(  G d d      Z)y)z$Certbot command line argument parser    N)Any)Dict)Iterable)List)Optional)Tuple)Union)crypto_util)errors)util)	constants)hooks)COMMAND_OVERVIEW)HELP_AND_VERSION_USAGE)SHORT_USAGE)add_domains)CustomHelpFormatter)flag_default)HelpfulArgumentGroup)set_test_server_options)	VERB_HELP)VERB_HELP_MAP)obj)disco)ArgumentSource)NamespaceConfigc            	          e Zd ZdZdee   dee   ddfdZg dZdefdZ	dee   d	e
eef   defd
ZdeddfdZdeeef   fdZdedej&                  fdZdefdZdeddfdZdeddfdZd$dZdedeee      de
eef   fdZdee
eee      ef      dededdfdZdee
eee      ef      dededej&                  fdZdededdfdZd%dedee   dede fd Z!de"jF                  ddfd!Z$d"e
eef   deee   ef   fd#Z%y)&HelpfulArgumentParserzArgparse Wrapper.

    This class wraps argparse, adding the ability to make --help less
    verbose, and request help on specific subcategories at a time, eg
    'certbot --help security' for security options.

    argspluginsreturnNc                    ddl m} i d|j                  d|j                  d|j                  d|j                  d|j
                  d|j                  d	|j                  d
|j                  d|j                  d|j                  d|j                  d|j                  d|j                  d|j                  d|j                  d|j                  d|j                   d|j"                  i| _        t'        j(                  t*        j,                        j.                  | _        g | _        g d}|t5        | j$                        | j6                  z   dgz   z  }t5        |      }||z   d gz   | _        || _        | j:                  r!| j:                  d   dk(  rd| j:                  d<   | j=                          | j?                  d| j8                        }| j?                  d| j8                        }|  tA        |tB              rtA        |tB              r|xs || _"        ntA        |tF              r|n|| _"        | jI                  || jD                        }| jK                  | jD                        | _&        i | _'        tQ        jR                  d|tT        ddgtW        d      djY                  dj[                  tW        d                         | _.        d!| j\                  _/        |  y )"Nr   )mainauthcertonlyruninstallr    registerupdate_accountshow_account
unregisterrenewrevokerollback
everythingupdate_symlinkscertificatesdeleteenhancereconfigure)allsecuritypaths
automationtestingmanagehelp--help-hcertbot-c--configconfig_filesz"path to config file (default: {0})z and )progusageformatter_classargs_for_setting_config_pathdefault_config_filesconfig_arg_help_messageF)0certbot._internalr#   r%   r&   r'   plugins_cmdr(   r)   r*   r+   r,   r-   r.   r0   r1   r2   r3   r4   VERBSdisplay_objNoninteractiveDisplaysysstdoutnotificationnotifyactionslistCOMMANDS_TOPICShelp_topicsr   determine_verbprescan_for_flag
isinstanceboolhelp_argstr_usage_stringdetermine_help_topicsvisible_topicsgroupsconfigargparse	ArgParserr   r   formatjoinparser_add_config_file_help)	selfr   r    r#   HELP_TOPICSplugin_nameshelp1help2short_usages	            ?/usr/lib/python3/dist-packages/certbot/_internal/cli/helpful.py__init__zHelpfulArgumentParser.__init__-   s   *
DMM

 488
 t||	

 t''
 
 d11
 D--
 $//
 TZZ
 dkk
 
 $((
 t33
 D--
  dkk!
" t||#
$ 4++%

, "77

CPP46 ,atDJJ'$*>*>>(KK,0M0;l0JdV0S	991/#DIIaL%%dD,<,<=%%h0@0@AeT"z%'>!NUDM%/s%;EDM(($--@"88G ;=$../*.
);!-n!=$H$O$O\.9:%<= -2)    )commandcommands
subcommandsubcommandsverbsc                     t        d t        D              }d}t        t              D ].  \  }}|j	                  dd      }|dj                  |||      z  }0 |dz  }|S )Nc              3   2   K   | ]  }t        |        y wN)len).0vs     rk   	<genexpr>z:HelpfulArgumentParser._list_subcommands.<locals>.<genexpr>w   s     4c!f4s   z,The full list of available SUBCOMMANDS is:

short z{0:<{length}}     {1}
)lengthzG
You can get more help on a specific subcommand with --help SUBCOMMAND
)maxr   sortedr   getra   )re   longesttextverbpropsdocs         rk   _list_subcommandsz'HelpfulArgumentParser._list_subcommandsv   sp    4m44?!), 	PKD%))GR(C-44T3w4OOD	P 	[[rm   rY   c                    d|v rd}nd}d|v rd}nd}t         }|du r;| j                  |t        ||fz  z   t        z          t	        j
                  d       |S || j                  v r9| j                  || j                         z          t	        j
                  d       |S |d	k(  r|t        ||fz  z  }|S t        |t              r,t        j                  |i       j                  d
d      }|r|n|}|S )a#  Make usage strings late so that plugins can be initialised late

        :param plugins: all discovered plugins
        :param help_arg: False for none; True for --help; "TOPIC" for --help TOPIC
        :rtype: str
        :returns: a short usage string for the top of --help TOPIC)
        nginxzH--nginx           Use the Nginx plugin for authentication & installationz+(the certbot nginx plugin is not installed)apachezI--apache          Use the Apache plugin for authentication & installationz,(the certbot apache plugin is not installed)Tr   r5   rC   N)r   rP   r   r   rM   exitrS   r   rW   rZ   r   r   )re   r    rY   	nginx_doc
apache_docrC   customs          rk   r[   z#HelpfulArgumentParser._usage_string   s    gbIEIwdJGJtKK 0J	3J JJMccdHHQK  ---KK 6 6 889HHQK   %Y(???E  #&"&&x488$GF$F%E rm   configc                     |j                   J |j                   d   t        j                  k(  r| j                  dk(  rg |_        yyy)z8Make "certbot renew" safe if domains are set in cli.ini.Ndomainsr,   )argument_sourcesr   CONFIG_FILEr   r   re   r   s     rk   &remove_config_file_domains_for_renewalz<HelpfulArgumentParser.remove_config_file_domains_for_renewal   sM     &&222##I..2L2LL		W$FN % Mrm   c                      j                   D ci c]  }|j                  t        j                   c} j                  j                         }dt        t        t        t        j                  t        f   f   dt        dd f fd}|D ].  }|j                  d      s |||   t        j                         0  ||j                  di       t        j                         d|v r|d   }|d   \  }}g }|D ]  }	|	j                  d	      s|	d
v rt        j                  d<   -d|	v r|	j!                  d      d   }	nd|	v r|	j!                  d      d   }	|	j                  d      r|j#                  |	       |	dd  D ]  }
|j#                  d	|
          |D ]0  }	 j%                  |	      }t        j                  |j                  <   2 S c c}w )Nsettings_dictsourcer!   c                     | j                         D cg c]  \  }\  }}|j                  |      n| }}}}j                  |D ci c]  }|j                  | c}       y c c}}}w c c}w ru   )items_find_action_for_argupdatedest)r   r   argaction_rQ   resultre   s         rk   update_resultz@HelpfulArgumentParser._build_sources_dict.<locals>.update_result   sz     0=/B/B/DF F+3 :@t005VS FG FMMgGFFKK/GHFGs   "A'A.config_fileenv_varcommand_liner{   -)r?   r@   
config_dir=r    --   )rQ   r   r   DEFAULTrc   get_source_to_settings_dictr   rZ   r   r_   Action
startswithr   r   ENV_VARCOMMAND_LINEsplitappendr   )re   r   source_to_settings_dictr   
source_keyr   r   unprocessed_argsr   r   	short_argr   s   `          @rk   _build_sources_dictz)HelpfulArgumentParser._build_sources_dict   s    FJ\\S66;; 6 66S #'++"I"I"K	Ic59N9NPS9S3T.T)U 	I"0	I59	I 2 	_J$$]35jA>C]C]^	_ 	-11)R@.BXBXY 443NCM$1"$5!Q D' 5~~c* ,,+9+F+FF<(#:))C.+CCZ))C.+C>>$'KK$ &)W 5	a	{O45)5.  B2237&4&A&Av{{#B
 y Ts   "Gr   c                     |d   dk7  rd|z   }| j                   D ]  }||j                  v s|c S  | j                   D ]*  }|j                  D ]  }|j                  |      s|c c S  , t        d| d      )Nr   r   r   z!Action corresponding to argument z is None)rQ   option_stringsr   AssertionError)re   r   r   option_strings       rk   r   z*HelpfulArgumentParser._find_action_for_arg   s     q6S=*C ll 	Ff+++	
 ll 	"F!'!6!6 " ++C0!M"	"
 @XNOOrm   c                    | j                   j                  | j                        }| j                  | j                     |_        | j                  |_        t        |      }|j                  | j                                | j                  |       | j                  dk(  rE|j                  r2t        j                  dj                  t        j                              d|_        |j                  r>|j                   r2t        j                  dj                  t        j                              |j"                  s|j$                  r| j'                  |       |j(                  r| j+                  |       |j,                  r|j.                  sd|_        |j0                  rt3        j0                  |       |j4                  r1t7        d |j8                  D              rt        j                  d      |j:                  r!|j<                  rt        j                  d      t?        |j@                  tB              r-tE        |j@                        dkD  rt        j                  d	      |S )
zParses command line arguments and returns the result.

        :returns: parsed command line arguments
        :rtype: configuration.NamespaceConfig

        r,   z{0} cannot be used with renewTz.Flag for non-interactive mode and {0} conflictc              3   F   K   | ]  }t        j                  |        y wru   )r   is_wildcard_domain)rw   ds     rk   ry   z3HelpfulArgumentParser.parse_args.<locals>.<genexpr>1  s     F!4**1-Fs   !zFUsing --allow-subset-of-names with a wildcard domain is not supported.z@Parameters --hsts and --auto-hsts cannot be used simultaneously.r   z8Only *one* --key-type type may be provided at this time.)#rc   
parse_argsr   rJ   r   funcr   set_argument_sourcesr   r   force_interactiver   Errorra   r   FORCE_INTERACTIVE_FLAGnoninteractive_modestagingdry_runset_test_servercsr
handle_csrmust_staplestaplevalidate_hooksr   allow_subset_of_namesanyr   hsts	auto_hstsrW   key_typerR   rv   )re   parsed_argsr   s      rk   r   z HelpfulArgumentParser.parse_args  s    kk,,TYY7::dii099 -##D$<$<$>?33F; 99''ll3::!88:; ; *.F&##(B(B,,@GG4467 7 >>V^^  (::OOF#fmm FM    (''Fv~~FFll $H I I ;;6++,,RT T foot,V__1E1I,,JL L rm   c                 .    t        | j                  |      S )zUpdates server, break_my_certs, staging, tos, and
        register_unsafely_without_email in config as necessary to prepare
        to use the test server.)r   r   r   s     rk   r   z%HelpfulArgumentParser.set_test_server?  s     'tyy&99rm   c                 j   |j                   dk7  rt        j                  d      |j                  rt        j                  d      |j                  dd \  }}t        j                  ||      \  }}}|D ]  }t        ||        |s%t        j                  d|j                  d   z        ||f|_        |D ch c]  }|j                          }	}t        |j                        }
|	|
k7  rCt        j                  dj                  dj                  |	      dj                  |
                  y	c c}w )
zProcess a --csr flag.r%   zCurrently, a CSR file may only be specified when obtaining a new or replacement via the certonly command. Please try the certonly command instead.z1--allow-subset-of-names cannot be used with --csrr      zJUnfortunately, your CSR %s needs to have a SubjectAltName for every domainzMInconsistent domain requests:
From the CSR: {0}
From command line/config: {1}z, N)r   r   r   r   r   r
   import_csr_filer   
actual_csrlowersetr   ConfigurationErrorra   rb   )re   r   csrfilecontentstypr   r   domainr   csr_domainsconfig_domainss              rk   r   z HelpfulArgumentParser.handle_csrE  s-   ;;*$,,  ; < < '',,RSS"JJqO'77JS'  	(F'	( ,,\**Q- ! ! !#J*12Qqwwy22V^^,.(++a		+.		.0IJL L ) 3s   9D0c                    d| j                   v sd| j                   v rd| _        yt        | j                         D ]G  \  }}|| j                  v s|}|dk(  rd}|dk(  rd}|| _        | j                   j	                  |        y d| _        y)	zDetermines the verb/subcommand provided by the user.

        This function works around some of the limitations of argparse.

        r=   r<   r;   Nr$   r%   r/   r&   )r   r   	enumeraterJ   pop)re   itokenr   s       rk   rU   z$HelpfulArgumentParser.determine_verbg  s     499DII 5DI!$)), 		HAu

"6>%D<' D 			a 		 	rm   flagpossible_argumentsc                     || j                   vry| j                   j                  |      }	 | j                   |dz      }||v r|S 	 y# t        $ r Y yw xY w)as  Checks cli input for flags.

        Check for a flag, which accepts a fixed set of possible arguments, in
        the command line; we will use this information to configure argparse's
        help correctly.  Return the flag's argument, if it has one that matches
        the sequence @possible_arguments; otherwise return whether the flag is
        present.

        Fr   T)r   index
IndexError)re   r   r   posnxts        rk   rV   z&HelpfulArgumentParser.prescan_for_flag  sk     tyy iiood#	))C!G$C((
 )   		s   A 	AAtopicskwargsc                 b    | j                   j                   | j                  |g|i |       y)a1  Add a new command line argument.

        :param topics: str or [str] help topic(s) this should be listed under,
                       or None for options that don't fit under a specific
                       topic which will only be shown in "--help all" output.
                       The first entry determines where the flag lives in the
                       "--help all" output (None -> "optional arguments").
        :param list *args: the names of this argument flag
        :param dict **kwargs: various argparse settings for this argument

        N)rQ   r   _add)re   r   r   r   s       rk   addzHelpfulArgumentParser.add  s+     	IDIIf>t>v>?rm   c                    |j                  d      }|t        j                  u r | j                  j                  |i |S t        |t              r | j                  |v r| j                  n|d   }n|}t        |t              sZ| j                  |   rK|| j                  v r!| j                  |   } |j                  |i |S  | j                  j                  |i |S t        j                  |d<    | j                  j                  |i |S )Nr   r   r;   )r   r   DeprecatedArgumentActionrc   add_argumentrW   rR   rY   rX   r]   r^   argparseSUPPRESS)re   r   r   r   r   topicgroups          rk   r   zHelpfulArgumentParser._add  s    H%T222 ,4;;++T<V<<fd# &*]]f%<DMM&)EE%&4+>+>u+E#E*)u))4:6::/t{{//@@@%..F6N+4;;++T<V<<rm   argument_namenum_argsc                 r    t        j                  | j                  d      }t        j                  |||       y)a  Adds a deprecated argument with the name argument_name.

        Deprecated arguments are not shown in the help. If they are used
        on the command line, a warning is shown stating that the
        argument is deprecated and no other action is taken.

        :param str argument_name: Name of deprecated argument.
        :param int num_args: Number of arguments the option takes.

        N)	functoolspartialr   r   add_deprecated_argument)re   r   r   add_funcs       rk   r   z-HelpfulArgumentParser.add_deprecated_argument  s,    2 $$TXXt4$$X}hGrm   r   rr   c                    | j                   |   rg | j                  j                  |fi || j                  |<   | j                  r1|D ],  }| j                  |   j                  |t        |   d          . t        | |      S )a  Create a new argument group.

        This method must be called once for every topic, however, calls
        to this function are left next to the argument definitions for
        clarity.

        :param str topic: Name of the new argument group.
        :param str verbs: List of subcommands that should be documented as part of
                          this help group / topic

        :returns: The new argument group.
        :rtype: `HelpfulArgumentGroup`

        rz   )r;   )r]   rc   add_argument_groupr^   rY   r   r   r   )re   r   rr   r   rx   s        rk   	add_groupzHelpfulArgumentParser.add_group  s      u%!?!?!?!P!PDKK}} WAKK&33AM!<LW<U3VW#D%00rm   c                     |j                         D ]>  \  }}| j                  ||j                        }|j                  j	                  ||       @ y)z

        Let each of the plugins add its own command line arguments, which
        may or may not be displayed as help topics.

        )descriptionN)r   r  long_description
plugin_clsinject_parser_options)re   r    name	plugin_epparser_or_groups        rk   add_plugin_argsz%HelpfulArgumentParser.add_plugin_args  sT      '}} 	NOD)"nnT9B9S9S - UO  66M	Nrm   chosen_topicc                     |dk(  rd}|dk(  rd}|dk(  r| j                   D ci c]  }||dk7  
 c}S |s| j                   D ci c]  }|d c}S | j                   D ci c]  }|||k(  
 c}S c c}w c c}w c c}w )z

        The user may have requested help on a topic, return a dict of which
        topics to display. @chosen_topic has prescan_for_flag's return type

        :returns: dict

        r$   r%   r/   r&   r5   zcertbot-route53:authF)rT   )re   r  ts      rk   r\   z+HelpfulArgumentParser.determine_help_topics  s     6!%L<' L5 <@<L<LMqAq222MM&*&6&67AuH77.2.>.>?1$$?? N7?s   A-
A2A7)r!   N) )&__name__
__module____qualname____doc__r   rZ   r   rl   rS   r   r	   rX   r[   r   r   r   r   r   r_   r   r   r   r   r   rU   r   rV   r   r   r   intr   r   r  r   PluginsRegistryr  r\   r  rm   rk   r   r   %   s.   DT#Y D# D4 DN TO	3 	!Xc] !eCI>N !SV !F _  QU  BT#~*=%> BHP P0E0E P.6O 6p:o :$ :L LT LD0S hxPS}>U #CI.,@(5hsm)<c)A#BC @C @@"@=8E$x}*=s*B$CD =S ==,33=>HS HC HD H81s 18C= 11$81.
Nu'<'< 
N 
N@%T	2B @#'t(;#<@rm   r   )*r  r   r   rM   typingr   r   r   r   r   r   r	   r_   r>   r
   r   r   rH   r   r   #certbot._internal.cli.cli_constantsr   r   r   certbot._internal.cli.cli_utilsr   r   r   r   r   certbot._internal.cli.verb_helpr   r   certbot._internal.displayr   rK   certbot._internal.pluginsr   certbot.configurationr   r   r   r  rm   rk   <module>r     sp    *   
            ' # @ F ; 7 ? 8 @ C 5 9 8 + 0 1r@ r@rm   