Skip to content

Global usage in help or help for subcommand #11

Closed

Description

@edmBernard

I try a simple example with the following class :

#! /usr/bin/python3
import fire

class Calculator(object):
    """A simple calculator class."""

    def add(self, first, second=123):
        print({"first": first, "second": second})
        return first + second

    def mul(self, first=5, second=123):
        print({"first": first, "second": second})
        return first * second

if __name__ == '__main__':
    fire.Fire(Calculator)

Is it possible to have access to help for command and subcommand ?

when I use -- --help flag on base script I only got :

$ ./test_fire.py -- --help
Type:        type
String form: <class '__main__.Calculator'>
File:        ~/my_folder/test_fire.py
Line:        5
Docstring:   A simple calculator class.

Usage:       ./test_fire.py

When I put no argument a got a better help message :

$ ./test_fire.py
Type:        Calculator
String form: <__main__.Calculator object at 0x7fdf898230b8>
File:        ~/my_folder/test_fire.py
Docstring:   A simple calculator class.

Usage:       ./test_fire.py
             ./test_fire.py add
             ./test_fire.py mul

Activity

  1. dbieber commented on Mar 6, 2017

    @dbieber
    Collaborator

    When you run './test_fire.py' the Calculator class is instantiated with no arguments and Fire shows you information about the result, which is a calculator object. This includes add and mul.

    When you run './test_fire.py -- --help' it's providing help for the Calculator class, rather than for an instantiated Calculator object. If you change your main to 'fire.Fire(Calculator())', then the same command will now show help for the calculator object, which would include usage information for add and mul.

    You'll see similar behavior if you run './test_fire.py mul -- --help' as compared to './test_fire.py mul'. The former prints help about the mul function without running it, whereas the latter runs the mul function and displays the result (615).

  2. dbieber commented on Mar 6, 2017

    @dbieber
    Collaborator

    I'll add that if you use a separator, you can force a function to be run in order to get help for the result.
    e.g. './test_fire.py - -- --help' will instantiate the Calculator object and then provide help about the result. Similarly, './test_fire.py mul - -- --help' will run the mul function and then provide help for the number 615!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions