3 گaQ@sdZddlZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z ddlm Z ddl Z dd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddl"m#Z#ddl"m$Z$ddl%m&Z'ddl(m)Z)ddl*m+Z+GdddZ,dS) z$Certbot command line argument parserN)Any)Dict)Iterable)List)Optional)Union) crypto_util)errors)util) constants)hooks)ARGPARSE_PARAMS_TO_REMOVE)COMMAND_OVERVIEW) EXIT_ACTIONS)HELP_AND_VERSION_USAGE) SHORT_USAGE)ZERO_ARG_ACTIONS)_Default) add_domains)CustomHelpFormatter) flag_default)HelpfulArgumentGroup) VERB_HELP) VERB_HELP_MAP)obj)disco)osc@szeZdZdZd2eeeeeddddZddd d d gZ ed d dZ eee eefedddZ e jddddZe jd ddZe jddddZe jddddZdd ddZeeee eefdddZee eeeefeedd d!d"Zeeeefd#d$d%Zeedd&d'd(Zffeeeeed)d*d+Zejdd,d-d.Z e eefeeefd/d0d1Z!dS)3HelpfulArgumentParserzArgparse 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. FN)argspluginsdetect_defaultsreturnc Csddlm}|j|j|j|j|j|j|j|j|j |j |j |j|j |j |j|jd|_tjtjj|_dddddg}|t|j|jd g7}t|}||dg|_||_||_|jr|jdd krd |jd<|j|jd |j}|jd |j}|t|trt|tr|p||_ nt|t!r,|n||_ |j"||j } |j#|j |_$i|_%i|_&t'j(d | t)ddgt*ddj+dj,t*dd|_-d|j-_.|dS)Nr)main)authcertonlyruninstallrregisterupdate_account unregisterrenewrevokerollback everythingupdate_symlinks certificatesdeleteenhanceallZsecuritypathsZ automationZtestingZmanagehelpz--helpz-hcertbotz-cz--configZ config_filesz"path to config file (default: {0})z and )progusageZformatter_classZargs_for_setting_config_pathZdefault_config_filesZconfig_arg_help_messageF)/certbot._internalr"r$r%r&Z plugins_cmdr'r(r)r*r+r,r.r/r0r1VERBS display_objZNoninteractiveDisplaysysstdoutZ notificationnotifylistCOMMANDS_TOPICS help_topicsr rdetermine_verbprescan_for_flag isinstanceboolhelp_argstr _usage_stringdetermine_help_topicsvisible_topicsgroupsdefaultsconfigargparseZ ArgParserrrformatjoinparserZ_add_config_file_help) selfrrr r"Z HELP_TOPICSZ plugin_namesZhelp1Zhelp2Z short_usagerQ/usr/lib/python3.6/helpful.py__init__0s^   zHelpfulArgumentParser.__init__ZcommandZcommandsZ subcommandZ subcommandsverbs)r!cCsXtddtD}d}x4ttD](\}}|jdd}|dj|||d7}q W|d7}|S) Ncss|]}t|VqdS)N)len).0vrQrQrR zsz:HelpfulArgumentParser._list_subcommands..z,The full list of available SUBCOMMANDS is: shortz{0:<{length}} {1} )ZlengthzG You can get more help on a specific subcommand with --help SUBCOMMAND )maxrsortedrgetrM)rPZlongesttextverbZpropsdocrQrQrR_list_subcommandsys z'HelpfulArgumentParser._list_subcommands)rrEr!cCsd|krd}nd}d|kr d}nd}t}|dkrV|j|t||fttjdnl||jkr~|j||jtjdnD|d kr|t||f7}n*t|t rt j |ij 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) ZnginxzH--nginx Use the Nginx plugin for authentication & installationz+(the certbot nginx plugin is not installed)ZapachezI--apache Use the Apache plugin for authentication & installationz,(the certbot apache plugin is not installed)Trr2r7N) rr=rrr;exitr?rarCrFrr])rPrrEZ nginx_docZ apache_docr7ZcustomrQrQrRrGs&     z#HelpfulArgumentParser._usage_string) parsed_argsr!cCsL|jdkrHx<|jjjD],\}}|jdrd|kr|jr>tng|_qWdS)z8Make "certbot renew" safe if domains are set in cli.ini.r*Z config_filedomainsN)r_rOZ_source_to_settingsitems startswithr rrd)rPrcsourceflagsrQrQrR&remove_config_file_domains_for_renewals zt"|j d kr>t j d |S) zParses command line arguments and returns the result. :returns: parsed command line arguments :rtype: argparse.Namespace cs i|]}tjjj||qSrQ)copydeepcopyrOZ get_default)rVkey)rPrQrR sz4HelpfulArgumentParser.parse_args..r*z{0} cannot be used with renewTz.Flag for non-interactive mode and {0} conflictcss|]}tj|VqdS)N)r Zis_wildcard_domain)rVdrQrQrRrXsz3HelpfulArgumentParser.parse_args..zFUsing --allow-subset-of-names with a wildcard domain is not supported.z@Parameters --hsts and --auto-hsts cannot be used simultaneously.z8Only *one* --key-type type may be provided at this time.)#rO parse_argsrr9r_funcrir varsrKZforce_interactiver ErrorrMr ZFORCE_INTERACTIVE_FLAGZnoninteractive_modestagingdry_runset_test_servercsr handle_csrZ must_stapleZstapleZvalidate_hooksr allow_subset_of_namesanyrdZhstsZ auto_hstsrCZkey_typer>rU)rPrcrQ)rPrRrpsH           z HelpfulArgumentParser.parse_argscCstdtjf}|jr(|j|kr(tjd|j|kr:tj|_|jr|jd krZtjd|jd|_ |_t j t j j |jtjdrd|_d|_dS) zGWe have --staging/--dry-run; perform sanity check and set config.serverserverz'--server value conflicts with --stagingr$r*zN--dry-run currently only works with the 'certonly' or 'renew' subcommands (%r)T*N)r$r*)rr Z STAGING_URIrtr{r rsrur_Zbreak_my_certsglobrpathrNZ config_dirZ ACCOUNTS_DIRZtosZregister_unsafely_without_email)rPrcZdefault_serversrQrQrRrvs      z%HelpfulArgumentParser.set_test_serverc Cs|jdkrtjd|jr$tjd|jdd\}}tj||\}}}x|D]}t||qNW|sxtjd|jd||f|_dd|D}t |j } || krtj d j d j |d j | d S) zProcess 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 --csrrzJUnfortunately, your CSR %s needs to have a SubjectAltName for every domaincSsh|] }|jqSrQ)lower)rVrnrQrQrR "sz3HelpfulArgumentParser.handle_csr..zMInconsistent domain requests: From the CSR: {0} From command line/config: {1}z, N)r_r rsryrwrZimport_csr_filerZ actual_csrsetrdZConfigurationErrorrMrN) rPrcZcsrfilecontentstyprwrdZdomainZ csr_domainsZconfig_domainsrQrQrRrxs&      z HelpfulArgumentParser.handle_csrcCs|d|jksd|jkrd|_dSxRt|jD]D\}}||jkr*|}|dkrLd}|dkrXd}||_|jj|dSq*Wd|_dS) zDetermines the verb/subcommand provided by the user. This function works around some of the limitations of argparse. z-hz--helpr4Nr#r$r-r%)rr_ enumerater9pop)rPitokenr_rQrQrRrA*s  z$HelpfulArgumentParser.determine_verb)flagpossible_argumentsr!c CsR||jkrdS|jj|}y|j|d}||kr6|SWntk rLYnXdS)asChecks 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. FroT)rindex IndexError)rPrrposZnxtrQrQrRrBBs  z&HelpfulArgumentParser.prescan_for_flag)topicsrkwargsr!cOs|jd}|tjkr&|jj||dSt|trJ|j|kr@|jn|d}n|}|jr`|j f|}t|t r|j |r||j kr|j |}|j||q|jj||nt j|d<|jj||dS)a1Add 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 actionNrr4)r]r ZDeprecatedArgumentActionrO add_argumentrCr>rEr #modify_kwargs_for_default_detectionrFrIrJargparseZSUPPRESS)rPrrrrtopicgrouprQrQrRaddWs       zHelpfulArgumentParser.add)rr!cKsP|jdd}|tkrL|tkr dnd|d<t|d<xtD]}|j|dq8W|S)a_Modify an arg so we can check if it was set by the user. Changes the parameters given to argparse when adding an argument so we can properly detect if the value was set by the user. :param dict kwargs: various argparse settings for this argument :returns: a modified versions of kwargs :rtype: dict rN store_trueZstoredefault)r]rrrr r)rPrrZparamrQrQrRrs    z9HelpfulArgumentParser.modify_kwargs_for_default_detection) argument_namenum_argsr!cCs tj|jd}tj|||dS)aAdds 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) functoolspartialrr add_deprecated_argument)rPrrZadd_funcrQrQrRrsz-HelpfulArgumentParser.add_deprecated_argument)rrTrr!cKsZ|j|rP|jj|f||j|<|jrPx(|D] }|j|j|t|ddq,Wt||S)aCreate 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` rY)r4)rIrOZadd_argument_grouprJrErrr)rPrrTrrWrQrQrR add_groups    zHelpfulArgumentParser.add_group)rr!cCs8x2|jD]&\}}|j||jd}|jj||q WdS)z Let each of the plugins add its own command line arguments, which may or may not be displayed as help topics. ) descriptionN)rerZlong_descriptionZ plugin_clsZinject_parser_options)rPrnameZ plugin_epZparser_or_grouprQrQrRadd_plugin_argss z%HelpfulArgumentParser.add_plugin_args) chosen_topicr!csXdkr ddkrddkr0dd|jDSsDdd|jDSfd d|jDS) 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%r2cSsi|]}|dk|qS)zcertbot-route53:authrQ)rVtrQrQrRrmsz?HelpfulArgumentParser.determine_help_topics..cSsi|] }d|qS)FrQ)rVrrQrQrRrmscsi|]}|k|qSrQrQ)rVr)rrQrRrms)r@)rPrrQ)rrRrHs z+HelpfulArgumentParser.determine_help_topics)F)"__name__ __module__ __qualname____doc__rrFrrDrSr?rarrGrZ NamespacerirprvrxrArBrrrrrintrrrrZPluginsRegistryrrHrQrQrQrRr(s&F #<"-  r)-rrrjrr}r;ZtypingrrrrrrrLr5rr r r8r r Z#certbot._internal.cli.cli_constantsr rrrrrZcertbot._internal.cli.cli_utilsrrrrrZcertbot._internal.cli.verb_helprrZcertbot._internal.displayrr:Zcertbot._internal.pluginsrZcertbot.compatrrrQrQrQrRsD