Giter VIP home page Giter VIP logo

Comments (8)

epsy avatar epsy commented on May 27, 2024 1

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 ifs detect it so your program doesn't create a log output. I suggest changing your ifs 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.

epsy avatar epsy commented on May 27, 2024 1

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.

Kristinita avatar Kristinita commented on May 27, 2024 1

@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.

edk0 avatar edk0 commented on May 27, 2024

from clize.

Kristinita avatar Kristinita commented on May 27, 2024

@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.

edk0 avatar edk0 commented on May 27, 2024

from clize.

Kristinita avatar Kristinita commented on May 27, 2024

😿

D:\SashaPythonista>python SashaLogbook.py --level=DEBUG
SashaLogbook.py: Unknown option '--level'. Did you mean '--help'?
Usage: SashaLogbook.py [level]

from clize.

Kristinita avatar Kristinita commented on May 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.