Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion fire/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ def Fire(component=None, command=None, name=None, serialize=None):
code 2. When used with the help or trace flags, Fire will raise a
FireExit with code 0 if successful.
"""
name = name or os.path.basename(sys.argv[0])
if not name:
script_name = sys.argv[0]
if os.path.basename(script_name) == '__main__.py':
# If invoked via `python -m package`, use the package name rather than
# the generic `__main__.py` filename.
name = os.path.basename(os.path.dirname(os.path.abspath(script_name)))
else:
name = os.path.basename(script_name)

# Get args as a list.
if isinstance(command, str):
Expand Down
11 changes: 11 additions & 0 deletions fire/fire_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ def testFireDefaultName(self):
stderr=None):
fire.Fire(tc.Empty)

def testFireDefaultNameDunderMain(self):
# When invoked as `python -m package`, sys.argv[0] is the path to the
# package's __main__.py file. The package name should be used instead of
# `__main__.py` in the usage output.
with mock.patch.object(sys, 'argv',
[os.path.join('python-fire', 'fire', 'mypackage',
'__main__.py')]):
with self.assertOutputMatches(stdout='SYNOPSIS.*mypackage',
stderr=None):
fire.Fire(tc.Empty)

def testFireNoArgs(self):
self.assertEqual(fire.Fire(tc.MixedDefaults, command=['ten']), 10)

Expand Down