Skip to content

Commit 9f5976c

Browse files
committed
Options: Print help text when using erroneous parameters
1 parent 39478cc commit 9f5976c

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Added basic example program about how to import a dashboard
99
- Options: Permitted usage without authentication credentials or token
1010
- Options: Do not assume defaults for `ACTION` and `--grafana_label` args
11+
- Options: Print help text when using erroneous parameters
1112

1213
## 0.4.0 (2024-10-16)
1314
- Fixed folder argument issue

grafana_import/cli.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ def main():
9797
# Get command line arguments.
9898
parser = argparse.ArgumentParser(description="play with grafana dashboards json files.")
9999

100+
def print_help_and_exit():
101+
parser.print_help(sys.stderr)
102+
parser.exit(1)
103+
100104
parser.add_argument(
101105
"-a",
102106
"--allow_new",
@@ -235,8 +239,8 @@ def main():
235239

236240
try:
237241
grafana_api = Grafana.Grafana(**params)
238-
except Exception as e:
239-
logger.error(f"ERROR: {e}")
242+
except Exception as ex:
243+
logger.error(str(ex))
240244
sys.exit(1)
241245

242246
# Import
@@ -347,7 +351,7 @@ def process_dashboard(file_path):
347351

348352
else:
349353
logger.error(f"Unknown action: {args.action}. Use one of: {parser._actions[-2].choices}")
350-
sys.exit(1)
354+
print_help_and_exit()
351355

352356

353357
if __name__ == "__main__":

0 commit comments

Comments
 (0)