google/python-fire

What's the meaning of "available commands: as_interger_ratio | bit_count |..."

Closed this issue · 2 comments

Save the code to calc.py

import fire


def double(number):
  """get number two times

  Args:
      number (_type_): input number

  Returns:
      _type_: result
  """
  return 2 * number

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

The correct usage is like: python calc.py 1

However, when I pass two parameters to it with the command python calc.py 1 10. It prints the information below:

ERROR: Could not consume arg: 2
Usage: calc.py 1 <command|value>
  available commands:    as_integer_ratio | bit_count | bit_length |
                         conjugate | from_bytes | is_integer | to_bytes
  available values:      denominator | imag | numerator | real

For detailed information on this command, run:
  calc.py 1 --help

What's the meaning of the commands: as_integer_ratio | bit_count | bit_length |conjugate | from_bytes | is_integer | to_bytes, and the value denominator | imag | numerator | real? How to use them?

I found several usages:

For example, bit_length

python calc.py 1                  # output: 2
python calc.py 1 bit_length # output: 2

python calc.py 12                   # output: 24
python calc.py 12 bit_length # output: 5

python calc.py 123                  # output: 246
python calc.py 123 bit_length # output: 8

python calc.py 1234                  # output: 2468
python calc.py 1234 bit_length # output: 12

But, I don't how bit_length works. The result seems not true.

Similarly, we can use other commands

# is_integer: it seems useful
python calc.py 12.2                   # output: 24.4
python calc.py 12.2 is_integer  # output: False

python calc.py 12.5                   # output: 25.0
python calc.py 12.5 is_integer  # output: True

# as_integer_ratio: I don't understand
python calc.py 12.2 as_integer_ratio  # output: [3433994715870003, 140737488355328]
python calc.py 12.5 as_integer_ratio  # output: [25, 1]

# bit_count: I don't understand
python calc.py 12.2 bit_count              # throw error
python calc.py 12.2 bit_count   2         # throw error
python calc.py 12.2 bit_count   "sdf"   # throw error
python calc.py 12.2 bit_count --help   # works. It shows the help about some commands:  as_integer_ratio, conjugate,hex, is_integer

Then, I use python calc.py 12.2 bit_count --help to check help information

NAME
    calc.py 12.2

SYNOPSIS
    calc.py 12.2 COMMAND | VALUE

COMMANDS
    COMMAND is one of the following:

     as_integer_ratio
       Return a pair of integers, whose ratio is exactly equal to the original float.

     conjugate
       Return self, the complex conjugate of any float.

     fromhex
       Create a floating-point number from a hexadecimal string.

     hex
       Return a hexadecimal representation of a floating-point number.

     is_integer
       Return True if the float is an integer.

VALUES
    VALUE is one of the following:

     imag

     real

OK, It told me I can use as_integer_ratio, conjugate,hex, and is_integer

However, there seems be some issues.

python calc.py 12.2 hex  # output: 0x1.8666666666666p+4
python calc.py 12.5 hex  # output: 0x1.9000000000000p+4
python calc.py 5 hex       # throw error! why?

Outputs with different data types have different available commands.

For example, you can see different commands help with the commands below.

python calc.py 1 --help     
python calc.py 1.1 --help   
python calc.py "abc" --help 
python calc.py abc --help