a
    Tsg_                     @   s   d dl mZ d dlmZ d dlmZ d dlZd dlZd dlZd dl	Zd dl
Zd dlZd dlZd dlZd dlZedZG dd dejZG dd	 d	ejZdS )
    )unicode_literals)__parse_specsNdnfc                       s   e Zd Z fddZ  ZS )MultilineHelpFormatterc                    s"   d|v r|  S tt| ||S )N
)
splitlinessuperr   _split_lines)selftextwidth	__class__ 9/usr/lib/python3.9/site-packages/dnf/cli/option_parser.pyr   '   s    z#MultilineHelpFormatter._split_lines)__name__
__module____qualname__r   __classcell__r   r   r   r   r   &   s   r   c                       s  e Zd ZdZd. fdd	Zdd ZG dd dejZG d	d
 d
ejZ	G dd dej
ZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd/ fd*d+	Zd0 fd,d-	Z  ZS )1OptionParserz5ArgumentParser like class to do things the "yum way".Tc                    s>   t t| jdtd d | _d | _|   |r:i | _t | _	d S )NF)add_helpZformatter_class)
r
   r   __init__r   command_positional_parsercommand_group_add_general_options
_cmd_usageset_cmd_groups)r   Zreset_usager   r   r   r   /   s    zOptionParser.__init__c                 C   s&   |    ttd| td dS )zOutput an error message, and exit the program.
           This method overrides standard argparser's error
           so that error output goes to the logger.

        :param msg: the error message to output
        zCommand line error: %s   N)print_usageloggerZcriticalr   sysexit)r   msgr   r   r   error9   s    zOptionParser.errorc                   @   s   e Zd Zdd ZdS )zOptionParser._RepoCallbackc                    s@   |dkrdnd t || j}| fddtd|D  d S )N--disablerepoZdisableenablec                 3   s   | ]}| fV  qd S Nr   ).0xZ	operationr   r   	<genexpr>H       z6OptionParser._RepoCallback.__call__.<locals>.<genexpr>\s*[,\s]\s*)getattrdestextendresplit)r   parser	namespacevaluesopt_strlr   r,   r   __call__E   s    z#OptionParser._RepoCallback.__call__Nr   r   r   r:   r   r   r   r   _RepoCallbackD   s   r<   c                   @   s   e Zd Zdd ZdS )z OptionParser._RepoCallbackEnablec                 C   s$   |j |d df t|d| d S )Nr   r(   Zreponame)repos_edappendsetattrr   r5   r6   r7   r8   r   r   r   r:   K   s    z)OptionParser._RepoCallbackEnable.__call__Nr;   r   r   r   r   _RepoCallbackEnableJ   s   rA   c                       s$   e Zd ZdZdZ fddZ  ZS )zOptionParser._SplitCallbackzN Split all strings in seq, at "," and whitespace.
        Returns a new list. r/   c                    s@   d}t | j|D ](}|s|r6ttj| |||| d}qd S )NTF)r3   r4   SPLITTERr
   r   _SplitCallbackr:   )r   r5   r6   r7   r8   firstvalr   r   r   r:   T   s    z$OptionParser._SplitCallback.__call__)r   r   r   __doc__rB   r:   r   r   r   r   r   rC   O   s   rC   c                   @   s   e Zd ZdZdd ZdS )z%OptionParser._SplitExtendDictCallbackz[ Split string at "," or whitespace to (key, value).
        Extends dict with {key: value}.c           	      C   sb   z| d\}}|r|stW n* tyH   td| }t| |Y n0 t|| j}|||< d S )N,zbad format: %s)r4   
ValueErrorr   argparseZArgumentErrorr0   r1   )	r   r5   r6   r7   r8   keyrE   r%   Zdctr   r   r   r:   b   s    z.OptionParser._SplitExtendDictCallback.__call__Nr   r   r   rF   r:   r   r   r   r   _SplitExtendDictCallback_   s   rL   c                   @   s   e Zd ZdZdd ZdS )zOptionParser._SetoptsCallbackzY Parse setopts arguments and put them into main_<setopts>
            and repo_<setopts>.c                 C   s  | d}t|dkr*ttd| d S t|dk rJttd| d S |\}}|d}|dkr|d | }	||d d  }t|dr|j}
ni }
|
|	i |g 	| t
|d	| j |
 n:t|d
r|j}ni }||g 	| t
|d| j | d S )N=   z'Setopt argument has multiple values: %sz Setopt argument has no value: %s.r    repo_setoptsZrepo_main_setoptsZmain_)r4   lenr"   Zwarningr   rfindhasattrrQ   
setdefaultr>   r?   r1   rR   )r   r5   r6   r7   r8   ZvalskvZperiodrepoZrepooptsZmainoptsr   r   r   r:   p   s,    



z&OptionParser._SetoptsCallback.__call__NrK   r   r   r   r   _SetoptsCallbackm   s   rZ   c                   @   s   e Zd Zdd ZdS )z'OptionParser.ParseSpecGroupFileCallbackc                 C   s   t || d S r)   r   r@   r   r   r   r:      s    z0OptionParser.ParseSpecGroupFileCallback.__call__Nr;   r   r   r   r   ParseSpecGroupFileCallback   s   r[   c                       s$   e Zd Z fddZdd Z  ZS )zOptionParser.PkgNarrowCallbackc              
      s   i | _ z"dD ]}|| | j |< ||= qW n8 ty` } z td| jj|f W Y d }~n
d }~0 0 g |d< ttj| j|i | d S )N)choicesdefaultz"%s() missing mandatory argument %sr]   )		pkgnarrowKeyError	TypeErrorr   r   r
   r   PkgNarrowCallbackr   )r   argskwargsrW   er   r   r   r      s    
z'OptionParser.PkgNarrowCallback.__init__c                 C   sT   | j d }|r |d | jd vr,| jd }n
|d}t||| t|| j | d S )NZ_actionr   r\   r]   )r1   r^   popr?   )r   r5   r6   r7   r8   Zdest_actionZnarrowr   r   r   r:      s    

z'OptionParser.PkgNarrowCallback.__call__)r   r   r   r   r:   r   r   r   r   r   ra      s   ra   c                   @   s   e Zd Zdd ZdS )zOptionParser.ForceArchActionc                 C   s   d|_ ||_d S )NT)Z
ignorearchZarchr@   r   r   r   r:      s    z%OptionParser.ForceArchAction.__call__Nr;   r   r   r   r   ForceArchAction   s   rf   c              
   C   s~  |  tdjtjjd}|jdddddtdd	 |jd
ddddtdd |jddddtdd |jdddtdjtjjdd |jdtddd |jdddgdtdd |jd d!dd"td#d$ |jd%d&g | jtd'd(d) |jd*d+g | jtd,d(d) |jd-dtd.d/ |jd0d1g | jtd2d3 |jd4d5ddtd6d |jd7d8d9dd:td;d< |jd=ddtd>d |	 }|jd?d@ddAdtdBdC |jdDd!dAtdEd< |jdFdGdHddtdId |jdJdKdLt
ddMtdNdO |jdPdQdRdSdtdTt
dU |jdVddtdWd |jdXdYddtdZd |jd[d\dt
td]d^ |jd_dd`dtdajtjjddb |jdcdtdddedf |jdgdhddtdid |jdjddtdkd |jdl| jdmg dntdodp |	 }|jdq| jdmg dntdrdp |jdsdtdndu| jg tdvdw |	 }|jdxdydzdtd{db |jd|dyd}dtd~db |jdddg d| jtddd |jddg d| jtddnd |jdi | jdtdd |jdd!ddtdd$ |jdd!ddtdd$ |jdddtdd |jdddtdd |jdddtdddd |jdddtdddd |jddddtdd |jddddytdd |jdddtdd |jddtdd |jddtdd |jddtdd |jddtdd |jdddg | jtdd3 |jddg d| jtddb |jddg d| jtddb |jddg dŢg d| jtdǃdȍ |jddtj| jttjj td˃d̍ |jddtjdύ dS )z0 Standard options known to all dnf subcommands. zGeneral {prog} options)progz-cz--configZconfig_file_pathNz[config file]zconfig file location)r1   r]   metavarhelpz-qz--quietquiet
store_truezquiet operation)r1   actionr]   ri   z-vz	--verbosezverbose operation)rl   r]   ri   z	--versionzshow {prog} version and exitz--installrootzset install rootz[path])ri   rh   z--nodocsstore_constZnodocsZtsflagszdo not install documentations)rl   constr1   ri   z--nopluginsZstore_falseZpluginszdisable all plugins)rl   r]   r1   ri   z--enablepluginZenablepluginzenable plugins by namez[plugin])r1   r]   rl   ri   rh   z--disablepluginZdisablepluginzdisable plugins by namez--releaseverz:override the value of $releasever in config and repo files)r]   ri   z--setoptZsetoptsz%set arbitrary config and repo options)r1   r]   rl   ri   z--skip-brokenZskip_brokenz.resolve depsolve problems by skipping packagesz-hz--helpz
--help-cmdri   zshow command help)rl   r1   ri   z--allowerasingz;allow erasing of installed packages to resolve dependenciesz-bz--bestZbestz8try the best available package versions in transactions.)rl   r1   r]   ri   z--nobestz2do not limit the transaction to the best candidatez-Cz--cacheonlyZ	cacheonlyz2run entirely from system cache, don't update cachez-Rz--randomwaitZ	sleeptimez	[minutes]zmaximum command wait time)r1   typer]   rh   ri   z-dz--debuglevelZ
debuglevelz[debug level]zdebugging output level)r1   rh   r]   ri   ro   z--debugsolverz)dumps detailed solving results into filesz--showduplicatesZshowdupesfromreposz2show duplicates, in repos, in list/search commandsz-ez--errorlevelzerror output level)r]   ro   ri   z--obsoletesZ	obsoleteszenables {prog}'s obsoletes processing logic for upgrade or display capabilities that the package obsoletes for info, list and repoquery)r]   r1   rl   ri   z--rpmverbosityzdebugging output level for rpmz[debug level name])r]   ri   rh   z-yz--assumeyesz*automatically answer yes for all questionsz
--assumenoz)automatically answer no for all questionsz--enablerepor=   z[repo]zTemporarily enable repositories for the purpose of the current dnf command. Accepts an id, a comma-separated list of ids, or a glob of ids. This option can be specified multiple times.)rl   r1   r]   rh   ri   r'   zTemporarily disable active repositories for the purpose of the current dnf command. Accepts an id, a comma-separated list of ids, or a glob of ids. This option can be specified multiple times, but is mutually exclusive with `--repo`.z--repoz--repoidrY   zUenable just specific repositories by an id or a glob, can be specified multiple times)rh   r1   rl   r]   ri   z--enableFZset_enabledz>enable repos with config-manager command (automatically saves)z	--disableZset_disabledz?disable repos with config-manager command (automatically saves)z-xz	--excludez--excludepkgsZexcludepkgsz exclude packages by name or globz	[package])r]   r1   rl   ri   rh   z--disableexcludesz--disableexcludepkgsZdisable_excludeszdisable excludepkgsz--repofrompathz[repo,path]zolabel and path to an additional repository to use (same path as in a baseurl), can be specified multiple times.)r]   rl   rh   ri   z--noautoremoveZclean_requirements_on_removez7disable removal of dependencies that are no longer usedz--nogpgcheckZgpgcheckz5disable gpg signature checking (if RPM policy allows)z--colorZcolorzcontrol whether color is used)r1   r]   ri   z	--refreshZfreshest_metadataz2set metadata as expired before running the command)r1   rl   ri   z-4Z
ip_resolvezresolve to IPv4 addresses onlyZipv4)r1   r]   ri   rl   rn   z-6zresolve to IPv6 addresses onlyZipv6z	--destdirz--downloaddirZdestdirz!set directory to copy packages toz--downloadonlyZdownloadonlyzonly download packagesz	--commentZcommentzadd a comment to transactionz--bugfixz,Include bugfix relevant packages, in updates)rl   ri   z--enhancementz1Include enhancement relevant packages, in updatesz--newpackagez0Include newpackage relevant packages, in updatesz
--securityz.Include security relevant packages, in updatesz
--advisoryz--advisoriesZadvisoryz=Include packages needed to fix the given advisory, in updatesz--bzz--bzsZbugzillaz7Include packages needed to fix the given BZ, in updatesz--cvez--cvesZcvesz8Include packages needed to fix the given CVE, in updatesz--sec-severityz--secseverity)ZCriticalZ	ImportantZModerateZMediumZLowZseverityzDInclude security relevant packages matching the severity, in updates)r\   r]   r1   rl   ri   z--forcearchZARCHz Force the use of an architecture)rh   r1   rl   r\   ri   command?)nargsri   )add_argument_groupr   formatr   utilZMAIN_PROG_UPPERadd_argumentrC   rZ   Zadd_mutually_exclusive_groupint	MAIN_PROGr<   rL   rI   ZSUPPRESSrf   sortedZrpmZ_BASEARCH_MAPkeys)r   Zgeneral_grpZ
best_groupZ
repo_groupZenable_groupr   r   r   r      s   




















z!OptionParser._add_general_optionsc                 C   sH   t j|j}t j|jd }|| jvrD||f| j|< | j| dS )z- store usage info about a single dnf command.r   N)r   Zi18nZucdsummaryaliasesr   r   add)r   cmdgroupr{   namer   r   r   _add_cmd_usagei  s
    
zOptionParser._add_cmd_usagec                 C   s"   t | D ]}| || qdS )z store name & summary for dnf commands

        The stored information is used build usage information
        grouped by build-in & plugin commands.
        N)r   r7   r   )r   Zcli_cmdsr   r~   r   r   r   add_commandsq  s    zOptionParser.add_commandsc                 C   s   t dt dd}dtjj }dD ]Z}|| jvr2q"|d||  7 }t| j D ]*}| j| \}}||krP|d||f 7 }qPq"|S )z- get the usage information to show the user. zList of Main Commands:zList of Plugin Commands:)mainZpluginz%s [options] COMMAND
z
%s

z	%-25s %s
)r   r   ru   rx   r   ry   r   rz   )r   Zdescusagegrpr   r   r{   r   r   r   	get_usagez  s    
zOptionParser.get_usagec                 C   s~   dt jj|jf | _|j| _tj| jdd| _	| j
| j	_
d | j	j_| d|j | _| j| j_|j| j_|| j d S )Nz%s %sF)r   z{} command-specific options)r   ru   rx   _basecmdrg   r{   descriptionrI   ArgumentParserr   r!   _positionalstitlers   rt   
capitalizer   cmd_add_argumentrv   _commandZset_argparserr   rp   r   r   r   _add_command_options  s    



z!OptionParser._add_command_optionsc                    sL   t  fdd|D r6t jj jg|R i |S  jj|i |S d S )Nc                    s   g | ]}|d   j v qS )r   )Zprefix_chars)r*   argr   r   r   
<listcomp>  r.   z1OptionParser.cmd_add_argument.<locals>.<listcomp>)allro   r   rv   r   )r   rb   rc   r   r   r   r     s     zOptionParser.cmd_add_argumentc                 C   s^   |D ]T}z| d W q tyV } z(tjtd|t|f W Y d }~qd }~0 0 qd S )Nzutf-8zCannot encode argument '%s': %s)encodeUnicodeEncodeErrorr   
exceptionsZConfigErrorr   str)r   rb   r   rd   r   r   r   _check_encoding  s    zOptionParser._check_encodingc                 C   s   |  | | |\}}|S r)   )r   parse_known_args)r   rb   r6   Z_unused_argsr   r   r   parse_main_args  s    
zOptionParser.parse_main_argsc                 C   s2   |  | | |\}}| j||}||_|jS r)   )r   r   r   
parse_argsZopts)r   rp   rb   r6   Zunused_argsr   r   r   parse_command_args  s
    
zOptionParser.parse_command_argsNc                    s,   | j r|  j| j j7  _tt| | d S r)   )r   _actionsr
   r   r!   )r   Zfile_r   r   r   r!     s    zOptionParser.print_usagec                    sb   |rF| j r| j j|jkr"| | |  j| jj7  _| j| jj n
| 	 | _
tt|   d S r)   )r   r   r   r   r   r   Z_action_groupsr>   r   r   r   r
   r   
print_helpr   r   r   r   r     s    

zOptionParser.print_help)T)N)N)r   r   r   rF   r   r&   rI   ZActionr<   rA   Z_AppendActionrC   rL   rZ   r[   ra   rf   r   r   r   r   r   r   r   r   r   r!   r   r   r   r   r   r   r   ,   s.   
 @	r   )Z
__future__r   Zdnf.i18nr   Zdnf.utilr   rI   Zdnf.exceptionsr   Zdnf.rpmZdnf.yum.miscZloggingZos.pathosr3   r#   Z	getLoggerr"   ZHelpFormatterr   r   r   r   r   r   r   <module>   s   
