Comments (8)
When you write def test(level="WARNING"):
, you get what Clize (and python) calls an optional positional argument. This means you can give it a value just on its own, like this:
$ python SashaLogbook.py ERROR
When you write python SashaLogbook.py level=ERROR
your program receives "level=ERROR"
. None if your if
s detect it so your program doesn't create a log output. I suggest changing your if
s to a chain of if-elif-elif-else
, with an else
that gives you an error message and exits. That way you can detect if the input is not exactly what you epected.
Now, if you want to be able to write this instead:
$ python SashaLogbook.py --level=ERROR
Then you need an option parameter:
def test(*, level="WARNING"):
...
It is documented here: http://clize.readthedocs.io/en/stable/basics.html#accepting-options (The page also links to how you can do this on Python 2 -- feel free to ask for details on this too)
from clize.
Ah, I just spotted another problem:
In your 3.
example, you use run(test)
and then print the log messages. By default, run()
exits the program with an appropriate exit code. You have to either move the log.X("...")
calls into def test
, or you can tell Clize not to exit: run(test, exit=False)
from clize.
@epsy , thank you!
Working example:
import logbook
import sys
log = logbook.Logger("Sasha Logbook")
def clize_log_level(*, logbook_level: 'll'="WARNING"):
"""Change log levels via command line.
User select, which logging messages to see. See about 6 log levels here:
https://logbook.readthedocs.io/en/stable/quickstart.html
:param logbook_level: user select logging level
"""
if logbook_level == "DEBUG":
logbook.StreamHandler(sys.stdout,
level=logbook.DEBUG).push_application()
if logbook_level == "WARNING":
logbook.StreamHandler(sys.stdout,
level=logbook.WARNING).push_application()
if logbook_level == "ERROR":
logbook.StreamHandler(sys.stdout,
level=logbook.ERROR).push_application()
run(clize_log_level, exit=False)
log.debug("Debug message")
log.warning("Warning message")
log.error("Error message")
from clize.
from clize.
@edk0 ,
You need to specify arguments like
--level=whatever
(or extend the parser, but using the standard format seems like a good idea)
I'm sorry, where, in what place I need to specify arguments? Can you show simple example?
Thanks.
from clize.
from clize.
😿
D:\SashaPythonista>python SashaLogbook.py --level=DEBUG
SashaLogbook.py: Unknown option '--level'. Did you mean '--help'?
Usage: SashaLogbook.py [level]
from clize.
All right, I understand, how worked simple example:
from clize import run
def echo(*, prefix='f'):
"""Echoes prefix parameter
:param prefix: Print specified letter
"""
print(prefix)
run(echo)
Output:
D:\SashaPythonista>python SashaClize.py --prefix=Example
Example
But what should I do next? What else should I read in order to perform my task?
Thanks.
from clize.
Related Issues (20)
- Use `Annotated` to add aliases to options HOT 4
- Errors when using dictionnary to specify alternate commands HOT 3
- Any way to give negative number as argument ? HOT 1
- `typing.List[str]` not supported by Clize
- [Question] Disable print(), if --help HOT 1
- Case-sensitive 1-character aliases are not allowed HOT 2
- How to support custom types and typing? HOT 4
- Support for Google Style Python Docstrings HOT 1
- make pathlib.Path a first citizen by adding default converter HOT 4
- Add intro for `repeated_test` in contribution guidelines
- KeyError when decorator rewrites varargs HOT 1
- Docs do not mention setup or setup.py
- Is this project still alive? HOT 1
- Missing 4.1.1 release HOT 1
- Requesting support for automatic shell completion
- Allow the creation of command aliases HOT 4
- Any issue with `attrs>=20.0.0`? HOT 2
- Proposing a PR to fix a few small typos
- repeated_test no longer compatible with 3.10 HOT 2
- What is going wrong here? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clize.