google/python-fire

How to use the avialable command "S.xx", such as S.count

Closed this issue · 2 comments

Put the code (copied from official document here) below to file hello.py

import fire


def hello(name):
  return 'Hello {name}!'.format(name=name)

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

I want to explore the commands provided by fire. I use the command to check help python Hello.py Bob --help

Then, I see the help like below

NAME                                                                       
    fire1.py abc - "abcabc"                                                
                                                                           
SYNOPSIS                                                                   
    fire1.py abc COMMAND                                                   
                                                                           
DESCRIPTION                                                                
    The string "abcabc"                                                    
                                                                           
COMMANDS                                                                   
    COMMAND is one of the following:                                       
                                                                           
     capitalize                                                            
       Return a capitalized version of the string.                         
                                                                           
     casefold                                                              
       Return a version of the string suitable for caseless comparisons.   
                                                                           
     center                                                                
       Return a centered string of length width.                           
                                                                           
     count                                                                 
       S.count(sub[, start[, end]]) -> int                                 
                                                                           
     encode                                                                
       Encode the string using the codec registered for encoding.          
                                                                           
     endswith                                                              
       S.endswith(suffix[, start[, end]]) -> bool                          
                                                                           
     expandtabs                                                            
       Return a copy where all tab characters are expanded using spaces.   
                                                                           
     find                                                                  
       S.find(sub[, start[, end]]) -> int                                  
                                                                           
     format                                                                
       S.format(*args, **kwargs) -> str     

...
...                               

I know the usage of some commands, such as capitalize, casefold,center.

python Hello.py Bob                         # output: Hello Bob!
python Hello.py Bob casefold           # output: hello bob!
python Hello.py Bob capitalize         # output: Hello bob!
# center is harder, but --help will tell you the usage
python Hello.py Bob center --help
python Hello.py Bob center 20 "="  # output: =====Hello Bob!=====

However, the other commands whose help says S.xxx seems not work, for example, count, endswith, find, and format

python Hello.py Bob count              # throw error
python Hello.py Bob count --help   # throw error

I don't know why count throws the errors

python Hello.py Bob count throw errors below:

Traceback (most recent call last):
  File "D:\Desktop\Hello.py", line 8, in <module>
    fire.Fire(hello)
  File "C:\Users\tanzi\scoop\apps\mambaforge\current\Lib\site-packages\fire\core.py", line 143, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tanzi\scoop\apps\mambaforge\current\Lib\site-packages\fire\core.py", line 477, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
                                ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tanzi\scoop\apps\mambaforge\current\Lib\site-packages\fire\core.py", line 693, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
                ^^^^^^^^^^^^^^^^^^^^^^
TypeError: count() takes at least 1 argument (0 given)

According to the message, I tried the command below. It works.

python Hello.py Bob                   # output: Hello Bob!
python Hello.py Bob count "o"  # output: 2
python Hello.py Bob count "h"  # output: 0
python Hello.py Bob count "H"  # output: 1
python Hello.py Bob count "Hello"  # output: 1

The usage is clear now. count is used to find str in the output string.

However, checking its help throws errors: python Hello.py Bob count "Hello" --help

Traceback (most recent call last):
  File "D:\Desktop\Hello.py", line 8, in <module>
    fire.Fire(hello)
  File "C:\Users\tanzi\scoop\apps\mambaforge\current\Lib\site-packages\fire\core.py", line 143, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tanzi\scoop\apps\mambaforge\current\Lib\site-packages\fire\core.py", line 477, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
                                ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tanzi\scoop\apps\mambaforge\current\Lib\site-packages\fire\core.py", line 693, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
                ^^^^^^^^^^^^^^^^^^^^^^
TypeError: count() takes no keyword arguments

The solution is clear now.

python Hello.py Bob count "Hello"  #  Output: 1
python Hello.py Bob endswith "b"  # Output: False
python Hello.py Bob endswith "!"   # Output: True
python Hello.py Bob find "B"           # Output: 6
python Hello.py Bob index "B"         # Output: 6
python Hello.py Bob find "Bob"       # Output: 6
python Hello.py Bob index "Bob"     # Output: 6