Skip to content

Cmd2ArgumentParser pass reference to state object for consumption by choices_provider functions #1280

Answered by anselor
gjvc asked this question in Q&A
Discussion options

You must be logged in to vote

Ah, so what we have here is an order of operations issue with how I originally implemented the argparse registration.

The cmd2.with_argparser() command decorators are executed during module load as the class definition is executed. The model instance doesn't exist until much later when you're instantiating the class. It is, therefore, impossible to pass anything on the cmd2 instance to the argparser or the decorator.

Pull Request #1278 may interest you as it is adds the ability to provide a factory function that is called when the cmd2 instance is created and therefore allows for future access to instance-specific attributes.

In the near-term there is a work-around you can do. You can def…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@anselor
Comment options

Answer selected by gjvc
@DM1122
Comment options

@anselor
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants