Let's say I need to view the argument names in the editor, it's usually done with cmd/ctrl + p and here's the view:
However when I try the same in the debugger console, I only get *args, **kwargs
How to change this behavior to be able to see the actual argument names?
script path/Module name
Click the list to select a type of target to run. Then, in the corresponding field, specify the path to the Python script or the module name to be executed.
Related
I have a Click app called "DC" starting with click.Group cli(), which has many subcommands. I'm trying to produce a text file with a list of all commands, arguments, options, and help text as a convenient reference. How do I introspect a Click application?
I experimented using the API reference, but it's confusing. Some of Command's methods (like get_usage()) require a "context" object as the first arg, and I only know two ways to get one:
Be inside a command with #pass_context decorator. (Not always the case.)
Call click.get_current_context() to get the "current" context, which seems to be the one attached to the bottom-level command that is currently being executed.
That seemed to work:
from dc.__main__ import cli
current_ctx = click.get_current_context()
click.echo(cli.get_usage(current_ctx))
This prints the docstring from the cli() function. However, if I try to inspect the list of subcommands:
click.echo(cli.commands)
I get an empty dict. After more exploring, I finally managed to find my way to the real data by doing this:
current_ctx.find_root().command.commands
which returned a dict with all the top-level commands I expected to see.
Is that the preferred method?
I have a Shell script, lets call it file.sh, that identifies a variable LOGFILE. I am trying to run a Python script calling the same variable, however it is not exporting properly.
I tried:
import os
print(os.environ(LOGFILE))
and got this error: name 'LOGFILE' is not defined
They are in the same directory, so I am not sure what the error is? How can I fix this?
The environment variable is not directly accessible as python variable. This is why you need to pass the name of the external environment-variable in the form of a string. Furthermore, environ is not a function, it is a dictionary, so you need to use square brackets to lookup 'LOGFILE' in the dictionary:
print(os.environ['LOGFILE'])
If you want a function that does something similar, use os.getenv()
print(os.getenv('LOGFILE'))
I have this chunk of code:
import click
#click.option('--delete_thing', help="Delete some things columns.", default=False)
def cmd_do_this(delete_thing=False):
print "I deleted the thing."
I would like to rename the option variable in --delete-thing. But python does not allow dashes in variable names. Is there a way to write this kind of code?
import click
#click.option('--delete-thing', help="Delete some things columns.", default=False, store_variable=delete_thing)
def cmd_do_this(delete_thing=False):
print "I deleted the thing."
So delete_thing will be set to the value of delete-thing
By default, click will intelligently map intra-option commandline hyphens to underscores so your code should work as-is. This is used in the click documentation, e.g., in the Choice example. If --delete-thing is intended to be a boolean option, you may also want to make it a boolean argument.
As gbe's answer says, click will automatically convert - in the cli parameters to _ for the python function parameters.
But you can also explicitly name the python variable to whatever you want. In this example, it converts --delete-thing to new_var_name:
import click
#click.command()
#click.option('--delete-thing', 'new_var_name')
def cmd_do_this(new_var_name):
print(f"I deleted the thing: {new_var_name}")
I'm using the click package for creating a command line tool. However, I would like to have a 'list' command. For example:
#click.command
#click.option(help='list...')
def list():
# do stuff here
Is there another way in click to pass in a command name other than having it as the function name? I don't want this function to shadow python's built in list. I've looked through the documentation and can't really find anything about command names -- I've read up on command aliases but that doesn't seem to help this problem. Or do I not need to worry about list being shadowed since it's being wrapped by the click decorator? Thanks in advance.
You can provide the name argument when you use the command decorator. Once you've done that, you can name your function whatever you want:
#click.command(name='list')
def list_command():
pass
See the Click documentation for details.
I am trying to debug a python application using pudb, everything is fine and good except that it's not displaying the instance variables (which we access with self.xxx). It just displays 1 variable called self. And it's of the original class type.
Even if I tell it to show after calling str(self), it still only displays the object information.
If you see the code has created many variables like self.parser, self.groups and I am not able to view/inspect any of them.
Is there a way to view all the instance variables of the current class while debugging using pudb?
This is the expected behaviour, and has nothing to do with your debugger: you only have one name, self.
To see its contents you can use dir(self).
See inspect complex variable in python debugger, like pudb
The short way: Highlight the variable and press backslash \ to toggle between the "Expanded" view of a variable in the variable inspection panel.
In this case we would just highlight self, and press \, which is just the Python variable representing the instance of the class.
Alternatively, press ENTER to open the "Variable Inspection Options" menu where at the bottom you can see the "Expanded" option.