python-fire icon indicating copy to clipboard operation
python-fire copied to clipboard

--help doesn't work on functions with **kwargs and all default args

Open ntjess opened this issue 5 years ago • 1 comments

Consider the following example:

import fire

def test(a=1, b=1):
  """
  Returns the sum of a and b.
  
  :param a: The first parameter
  :param b: The second parameter
  """
  return a + b
  
fire.Fire(test)

If the file is called using python ./myFile.py --help, the help signature is printed as expected

However, if **kwargs is added to the signature of test, the return value of the function is printed instead. Is this expected behavior? It works if I run python ./myFile.py -- --help, but for someone using my application and not familiar with the 'fire' CLI, this is non-intuitive.

How do I need to modify test so that if someone calls --help on it, the help signature is printed?

Thanks!

ntjess avatar May 17 '20 17:05 ntjess

The way Fire handles -- is indeed odd. GNU uses a bare double-dash to indicate the end of options and the beginning of positionals. POSIX also mentions this in their Utility Conventions:

Guideline 10: The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the '-' character.

A hacky workaround I have used in the past was to force --help to always be treated as a Fire flag, discarding any arguments following it:

def _SeparateFlagArgs(args):
    try:
        index = args.index('--help')
        args = args[:index]
        return args, ['--help']
    except ValueError:
        return args, []
    
fire.core.parser.SeparateFlagArgs = _SeparateFlagArgs

melsabagh avatar Sep 26 '21 19:09 melsabagh