abseil/abseil-py

argparse_flags.ArgumentParser does not accept --flagfile if there are no flags

jacky8hyf opened this issue · 3 comments

Minimum repro:

# foo.py
import absl.flags.argparse_flags
import argparse
# absl.flags.DEFINE_string("do_not_use_flagfile_hack", "", "") # A
parser = absl.flags.argparse_flags.ArgumentParser()
parser.add_argument("--foo")
parser.parse_args()
echo "--foo" > flags.txt
foo.py --flagfile=flags.txt

The error is:

foo.py: error: unrecognized arguments: --flagfile=flags.txt

The command would work if I uncomment line A.

It appears that ArgumentParser only accepts --flagfile if there are some DEFINE_'d strings. This is because of this line:

https://github.com/abseil/abseil-py/blob/main/absl/flags/argparse_flags.py#L156

    if self._inherited_absl_flags: # <---
      # Handle --flagfile.
      # Explicitly specify force_gnu=True, since argparse behaves like
      # gnu_getopt: flags can be specified after positional arguments.
      args = self._inherited_absl_flags.read_flags_from_files(
          args, force_gnu=True)

--flagfile is only accepted if bool(self._inherited_absl_flags), which is False if there are no DEFINE_d strings.

yilei commented

Thanks for the bug report and analysis! You are right, the code should check if self._inherited_absl_flags is not None instead. I'll try that.

Hi @yilei are there any updates?

yilei commented

@jacky8hyf Thanks for the ping! I picked this up again now.