Skip to content

take defaults and types from keyword arguments #1466

@tmbdev

Description

@tmbdev

Consider the following little program:

import click                                                                    
                                                                                
@click.command()                                                                
#@click.option("--arg")  #(1)                                                       
@click.option("--arg", default=17)  #(2)
def command(arg=19):                                                            
    print(arg, type(arg))                                                       
                                                                                
if __name__=='__main__':                                                        
    command()   

Here, if using declaration (1), passing an argument with --arg will result in the passing of a string argument. On the other hand, declaration (2) overrides the default and forces the programmer to repeat defaults.

It would seem to be nicer behavior if @click.option used reflection to get the default value from keyword arguments, removing the need for duplication of the default and avoiding potential type inconsistencies.

(Perhaps to avoid backwards incompatibility where people rely on the odd current behavior, the fix could use a slight variant, say, @click.opt. When used with default=, it would just behave as the current options.)

import click                                                                    
                                                                                
@click.command()
@click.opt("--arg", help="defaults to '19' of type 'int'")
def command(arg=19):
    print(arg, type(arg))
     
if __name__=='__main__':
    command()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions