Code Monkey home page Code Monkey logo

Comments (2)

jonaslb avatar jonaslb commented on June 7, 2024

I'm not sure using typer like that on an init method is actually supported. I think having your CLI be a simple method is really to be preferred. Try putting the CLI into its own method, and then call whatever program logic you have from there, like this for example:

import typer
from typing import Optional
from datetime import datetime

class Some:
    # your class impl
    pass

def main(start_date: Optional[datetime] = None, stop_date: Optional[datetime] = None):
    Some(start_date, stop_date).run()

if __name__ == "__main__":
    typer.run(main)

from typer.

kuykendall-ben avatar kuykendall-ben commented on June 7, 2024

You can totally use a constructor as a typer command. However, it's pretty confusing! I would recommend @jonaslb's approach above instead. But if you really want to... the trick is to decorate the class instead of the initializer:

from typing import Optional
import typer

app = typer.Typer()

@app.command()
class Some:
    def __init__(self, start_date: Optional[str] = None, stop_date: Optional[str] = None) -> None:
        self.start = start_date
        self.stop = stop_date
        self.run()

    def run(self):
        print(f"{self.start} - {self.stop}")

if __name__ == "__main__":
    app()

Then

$ /usr/bin/python3 main.py --start-date 2023-01-01 --stop-date 2023-12-31
2023-01-01 - 2023-12-31

However, your code as written has a few other problems. The biggest one to me is trying to use the return value of the typer entry point. You shouldn't run any code after calling app(). Make sure all the code you want to execute is inside your command.

from typer.

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.