[Question] Changing variable from command line
See original GitHub issue1. Briefly
I don’t understand, how I can change one variable from command line.
2. Settings
1. Environment
- Windows 10 64-bit Enterprise EN,
- Python 3.6.3,
- Clize 4.0.2.
2. File
For example, I have a file, where I use Logbook logging.
import logbook
import sys
logbook.StreamHandler(sys.stdout,
level=logbook.WARNING).push_application()
log = logbook.Logger("Sasha Logbook")
log.debug("Debug message")
log.warning("Warning message")
log.error("Error message")
If I run this file in console, I get output:
D:\SashaPythonista>python SashaLogbook.py
[2018-01-01 17:55:47.063625] WARNING: Sasha Logbook: Warning message
[2018-01-01 17:55:47.064624] ERROR: Sasha Logbook: Error message
Output, if logbook.StreamHandler(sys.stdout, level=logbook.DEBUG).push_application()
:
[2018-01-01 17:55:46.994054] DEBUG: Sasha Logbook: Debug message
[2018-01-01 17:57:26.982679] WARNING: Sasha Logbook: Warning message
[2018-01-01 17:57:26.983677] ERROR: Sasha Logbook: Error message
And so on.
2. Expected behavior
If:
D:\SashaPythonista>python SashaLogbook.py level=DEBUG
[2018-01-01 17:55:46.994054] DEBUG: Sasha Logbook: Debug message
[2018-01-01 17:55:47.063625] WARNING: Sasha Logbook: Warning message
[2018-01-01 17:55:47.064624] ERROR: Sasha Logbook: Error message
If:
D:\SashaPythonista>python SashaLogbook.py level=WARNING
[2018-01-01 17:57:26.982679] WARNING: Sasha Logbook: Warning message
[2018-01-01 17:57:26.983677] ERROR: Sasha Logbook: Error message
And so on.
3. Actual behavior
I try to add Clize:
import logbook
import sys
from clize import run
log = logbook.Logger("Sasha Logbook")
def test(level="WARNING"):
if level == "DEBUG":
logbook.StreamHandler(sys.stdout,
level=logbook.DEBUG).push_application()
if level == "WARNING":
logbook.StreamHandler(sys.stdout,
level=logbook.WARNING).push_application()
if level == "ERROR":
logbook.StreamHandler(sys.stdout,
level=logbook.ERROR).push_application()
run(test)
log.debug("Debug message")
log.warning("Warning message")
log.error("Error message")
If I replace run(test)
to test()
, Logbook messages print to console. But if I use run(test)
, I haven’t output in console.
D:\SashaPythonista>python SashaLogbook.py --help
Usage: SashaLogbook.py [level]
Arguments:
level (default: WARNING)
Other actions:
-h, --help Show the help
D:\SashaPythonista>python SashaLogbook.py level=DEBUG
D:\SashaPythonista>python SashaLogbook.py level=ERROR
4. Did not help
- I see example for logging, but it hard for me. I want only control logging levels from command line.
5. Do not offer
- I want to use Logbook, not default Python logging module.
Thanks.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
loop a command with a changing variable in Windows ...
bat file I'm creating (sorts out movies into another alphabetized folder) so that it will have far fewer lines. I want to get...
Read more >How to replace bash variable with command line arguments
1. Replace that with abc=abc ./bash.sh to set abc as an environment variable instead of a positional argument (which nothing in your script ......
Read more >Changing Variable at the command line of a bash script
This would set the value of the variable value to whatever the first command line argument was, unless it was not provided, in...
Read more >How to change the value of a variable in a section of a file ...
I am looking for help with an example usage of the 'sed' command. i want to change value of variable in section in...
Read more >How do I show a changing variable value in command ...
My concern is, I want to display the result ONLY IN ONE ROW, that is, "Iteration =" does not show again, ONLY the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@epsy , thank you!
Working example:
Ah, I just spotted another problem:
In your
3.
example, you userun(test)
and then print the log messages. By default,run()
exits the program with an appropriate exit code. You have to either move thelog.X("...")
calls intodef test
, or you can tell Clize not to exit:run(test, exit=False)