Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

display commands in a particular order #298

Open
wiso opened this issue Nov 24, 2020 · 14 comments
Open

display commands in a particular order #298

wiso opened this issue Nov 24, 2020 · 14 comments

Comments

@wiso
Copy link

wiso commented Nov 24, 2020

In the help Fire displays commands in alphabetical order. It would be nice to be able to show them in a custom order specified by the user.

@vinayak-parab
Copy link

@wiso can you please share some more information about how the current help result shows up and how would you like to customize it with an example or sample screenshots ?

@wiso
Copy link
Author

wiso commented Dec 18, 2020

sure, actually is is very simple. This is the help I get

NAME
    run.py

SYNOPSIS
    run.py - COMMAND | VALUE

COMMANDS
    COMMAND is one of the following:

     background_prefit

     cfactor_sys

     check_workspace

     compare_fit_sys

     fit

     fit_nominal

     fit_sysgroup

     make_workspace

     make_xml

     matrix_sys

     plot_results

     plot_workspace

     signal_model_nominal
       run the signal shape model for the nominal case

     signal_model_systematic
       run the signal shape model for the systematic case, fixing most of the parameters to the nominal ones

VALUES
    VALUE is one of the following:

     basedir

     debug

     quantities

     regions

the commands are displayed in alphabetical order. I would like to customize that order.

@vinayak-parab
Copy link

Hello @wiso
I was able to reproduce the issue on my machine.

import fire

def hello(name="World"):
return "Hello %s!" % name

if name == 'main':
fire.Fire(hello)

when I run the code with the following command : python firecli.py vinayak --help , it shows the list of commands that could be used in alphabetical order (screenshot attached). However, I do not see two different options where it displays the commands and values. How would you like the command display options to be customized ? Can you please elaborate on this part ?
image

@wiso
Copy link
Author

wiso commented Dec 18, 2020

There is no issue of any kind. My point is just that I would like to show then list of actions in an order defined by me, and not by alphabetical order.

@vinayak-parab
Copy link

Ok got it. I understood your point.

@varunjohn786
Copy link

Option 1: When reordering sequence is specified using command line.

File-name = fire_test.py

import fire

class CustomSequence(object):
    def __init__(self, offset=1):
        self.offset = offset

    def generate(self, start, stop):
        return ' '.join(str(item) for item in range(start, stop, self.offset))

    def generate2(self):
        print("Function 1")

    def generated(self):
        print("Function 2")

    def cool(self):
        return "Function 3"
    def cool_for(self):
        print("Function 4")

    def rush_hour(self):
        print("Function 5")


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

Command line input with default sequence:

python fire_test.py --help

Output:

INFO: Showing help with the command 'fire_test.py -- --help'.

NAME
    fire_test.py

SYNOPSIS
    fire_test.py COMMAND | VALUE

COMMANDS
    COMMAND is one of the following:

     cool

     cool_for

     generate

     generate2

     generated

     rush_hour

VALUES
    VALUE is one of the following:

     offset

Command line input when a user specifies a sequence:

python fire_test.py --help rush_hour generate2

Output:

INFO: Showing help with the command 'fire_test.py -- --help'.

NAME
    fire_test.py

SYNOPSIS
    fire_test.py COMMAND | VALUE

COMMANDS
    COMMAND is one of the following:

     rush_hour

     generate2

     cool

     cool_for

     generate

     generated

VALUES
    VALUE is one of the following:

     offset

Option 2: When reordering sequence is specified in the file (fire_test.py in this case)

import fire

class CustomSequence(object):
    def __init__(self, offset=1):
        self.offset = offset

    def generate(self, start, stop):
        return ' '.join(str(item) for item in range(start, stop, self.offset))

    def generate2(self):
        print("Function 1")

    def generated(self):
        print("Function 2")

    def cool(self):
        return "Function 3"
    def cool_for(self):
        print("Function 4")

    def rush_hour(self):
        print("Function 5")

if __name__ == '__main__':
""" New paramter for the sequence has been added over here, which gets passed 
on through multiple calls to _MakeUsageDetailsSection function in helptext.py """
    fire.Fire(CustomSequence(), help_sequence = ["rush_hour","generate2"])

Output:

INFO: Showing help with the command 'fire_test.py -- --help'.

NAME
    fire_test.py

SYNOPSIS
    fire_test.py COMMAND | VALUE

COMMANDS
    COMMAND is one of the following:

     rush_hour

     generate2

     cool

     cool_for

     generate

     generated

VALUES
    VALUE is one of the following:

     offset

Please let me know either option 1 or 2 resolves the issue or is something else expected ?

@wiso
Copy link
Author

wiso commented Jan 8, 2021

Thank you. I haven't tested, but the last solution seems to do what I want in the simplest way.

@varunjohn786
Copy link

varunjohn786 commented Jan 8, 2021

Please validate the changes and test it using the code in my forked repository branch. Since the code mentioned in above comments does not contain all the changes required in core.py and helptext.py files.
Fire_test.py file for the testing will remain same from above comments or you can create your own script. You will just need to add a new parameter help_sequence while calling the Fire function

Link to the branch

@wiso
Copy link
Author

wiso commented Jan 9, 2021

your example works only if I run fire_test.py -- --help or fire_test.py --help or fire_test.py -h. If I run fire_test.py I get the alphabetical order.

@varunjohn786
Copy link

My initial understanding was that the reordering was required in only --help functionality of python-fire. But now, I have made changes to the code so that it now gives the reordered list of commands if you run python fire_test.py also.

Link to branch with code

@wiso
Copy link
Author

wiso commented Jan 9, 2021

ok, thanks.

There is a problem, I have added a documentation string in one function and the order is not correct

import fire

class CustomSequence(object):
    def __init__(self, offset=1):
        self.offset = offset

    def generate(self, start, stop):
        return ' '.join(str(item) for item in range(start, stop, self.offset))

    def generate2(self):
        print("Function 1")

    def generated(self):
        print("Function 2")

    def cool(self):
        return "Function 3"
    def cool_for(self):
        print("Function 4")

    def rush_hour(self):
        "zzzz"
        print("Function 5")

if __name__ == '__main__':
    """ New paramter for the sequence has been added over here, which gets passed 
on through multiple calls to _MakeUsageDetailsSection function in helptext.py """
    fire.Fire(CustomSequence(), help_sequence = ["rush_hour","generate2"])

@varunjohn786
Copy link

Thanks for pointing out the error. I have made changes to the code so that now documentation strings don't affect the reordering of the commands. I have also made changes so that every occurrence where commands get displayed as help will have this reordering.

Link to Branch

@wiso
Copy link
Author

wiso commented Jan 14, 2021

Thank, it works!

@varunjohn786
Copy link

Thanks for the confirmation. I will soon raise a pull request with the change.

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

No branches or pull requests

4 participants