Skip to content

Commit 77e665c

Browse files
committed
fixed bugs
1 parent 9f42e7a commit 77e665c

File tree

6 files changed

+165
-124
lines changed

6 files changed

+165
-124
lines changed

astroquery_cli/main.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def load_default_lang():
5454

5555
app = typer.Typer(
5656
name="aqc",
57-
help=i18n._("Astroquery CLI"),
57+
help=builtins._("Astroquery CLI"),
5858
invoke_without_command=True,
5959
no_args_is_help=False,
6060
add_completion=False, # Set to False to remove global completion commands
@@ -90,7 +90,7 @@ def setup_subcommands():
9090
app.add_typer(sdss_cli.get_app(), name="sdss")
9191
app.add_typer(eso_cli.get_app(), name="eso")
9292
app.add_typer(nist_cli.get_app(), name="nist")
93-
app.command(name="exoplanet", help=builtins._("Query the NASA Exoplanet Archive."))(exoplanet_cli.get_app())
93+
app.add_typer(exoplanet_cli.get_app(), name="exoplanet")
9494

9595
@app.callback()
9696
def main_callback(
@@ -99,7 +99,7 @@ def main_callback(
9999
None,
100100
"-l",
101101
"--lang",
102-
help=i18n._("Set the language for output messages (e.g., 'en', 'zh'). Affects help texts and outputs."),
102+
help=builtins._("Set the language for output messages (e.g., 'en', 'zh'). Affects help texts and outputs."),
103103
is_eager=True,
104104
envvar="AQC_LANG",
105105
show_default=False
@@ -108,26 +108,26 @@ def main_callback(
108108
False,
109109
"-p",
110110
"--ping",
111-
help=i18n._("Test connectivity to major services (only available at top-level command).")
111+
help=builtins._("Test connectivity to major services (only available at top-level command).")
112112
),
113113
field: bool = typer.Option(
114114
False,
115115
"-f",
116116
"--field",
117-
help=i18n._("Test field validity for modules (only available at top-level command).")
117+
help=builtins._("Test field validity for modules (only available at top-level command).")
118118
),
119119
debug: bool = typer.Option(
120120
False,
121121
"-d",
122122
"--debug",
123-
help=i18n._("Enable debug mode with verbose output."),
123+
help=builtins._("Enable debug mode with verbose output."),
124124
envvar="AQC_DEBUG"
125125
),
126126
verbose: bool = typer.Option(
127127
False,
128128
"-v",
129129
"--verbose",
130-
help=i18n._("Enable verbose output.")
130+
help=builtins._("Enable verbose output.")
131131
)
132132
):
133133
_ = builtins._
@@ -281,7 +281,7 @@ def cli():
281281
debug_manager.enable_debug()
282282
# Print a message indicating debug mode is enabled
283283
console = Console()
284-
console.print("[bold green]Debug mode enabled.[/bold green]")
284+
console.print(_("[bold green]Debug mode enabled.[/bold green]"))
285285
# Removed the "Debug mode disabled" message as per user request.
286286

287287
setup_subcommands()

astroquery_cli/modules/eso_cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ def eso_callback(
3535
False,
3636
"-t",
3737
"--debug",
38-
help=_("Enable debug mode with verbose output."),
38+
help=builtins._("Enable debug mode with verbose output."),
3939
envvar="AQC_DEBUG"
4040
),
4141
verbose: bool = typer.Option(
4242
False,
4343
"-v",
4444
"--verbose",
45-
help=_("Enable verbose output.")
45+
help=builtins._("Enable verbose output.")
4646
)
4747
):
4848
setup_debug_context(ctx, debug, verbose)
@@ -61,7 +61,7 @@ def eso_callback(
6161
if commands_match:
6262
commands_section = commands_match.group(0)
6363
filtered_commands_section = "\n".join([
64-
line for line in commands_section.splitlines() if "Usage:" not in line
64+
line for line in commands_section.splitlines() if _("Usage:") not in line
6565
])
6666
console.print(filtered_commands_section)
6767
else:
@@ -118,7 +118,7 @@ def query_eso(
118118
handle_astroquery_exception(ctx, e, _("ESO query"))
119119
raise typer.Exit(code=1)
120120

121-
@app.command(name="list-instruments", help=builtins._("List available ESO instruments."))
121+
@app.command(name="list", help=builtins._("List available ESO instruments."))
122122
@global_keyboard_interrupt_handler
123123
def list_instruments(ctx: typer.Context):
124124
console.print(f"[cyan]{_('Listing ESO instruments...')}[/cyan]")

astroquery_cli/modules/exoplanet_cli.py

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@ def get_app():
2222
import builtins
2323
_ = builtins._
2424

25-
@global_keyboard_interrupt_handler
26-
def exoplanet_command(
25+
exoplanet_app = typer.Typer(
26+
name="exoplanet",
27+
help=builtins._("Query the NASA Exoplanet Archive."),
28+
invoke_without_command=True,
29+
no_args_is_help=False
30+
)
31+
32+
@exoplanet_app.callback()
33+
def exoplanet_main_callback(
2734
ctx: typer.Context,
28-
planet_name: str = typer.Argument(..., help=builtins._("Planet name (e.g., 'Kepler-186 f').")),
29-
output_file: Optional[str] = common_output_options["output_file"],
30-
output_format: Optional[str] = common_output_options["output_format"],
31-
max_rows_display: int = typer.Option(
32-
25, help=builtins._("Maximum number of rows to display. Use -1 for all rows.")
33-
),
34-
show_all_columns: bool = typer.Option(
35-
False, "--show-all-cols", help=builtins._("Show all columns in the output table.")
36-
),
3735
debug: bool = typer.Option(
3836
False,
3937
"-t",
@@ -50,6 +48,44 @@ def exoplanet_command(
5048
):
5149
setup_debug_context(ctx, debug, verbose)
5250

51+
if ctx.invoked_subcommand is None and \
52+
not any(arg in ["-h", "--help"] for arg in ctx.args):
53+
# Capture the full help output by explicitly calling the app with --help
54+
help_output_capture = StringIO()
55+
with redirect_stdout(help_output_capture):
56+
try:
57+
exoplanet_app(ctx.args + ["--help"])
58+
except SystemExit:
59+
pass
60+
full_help_text = help_output_capture.getvalue()
61+
62+
# Extract only the "Commands" section using regex, including the full bottom border
63+
commands_match = re.search(r'╭─ Commands ─.*?(\n(?:│.*?\n)*)╰─.*─╯', full_help_text, re.DOTALL)
64+
if commands_match:
65+
commands_section = commands_match.group(0)
66+
filtered_commands_section = "\n".join([
67+
line for line in commands_section.splitlines() if "Usage:" not in line
68+
])
69+
console.print(filtered_commands_section)
70+
else:
71+
console.print(full_help_text)
72+
raise typer.Exit()
73+
74+
@exoplanet_app.command(name="query", help=builtins._("Query the NASA Exoplanet Archive for a specific planet."))
75+
@global_keyboard_interrupt_handler
76+
def exoplanet_query_command(
77+
ctx: typer.Context,
78+
planet_name: str = typer.Argument(..., help=builtins._("Planet name (e.g., 'Kepler-186 f').")),
79+
output_file: Optional[str] = common_output_options["output_file"],
80+
output_format: Optional[str] = common_output_options["output_format"],
81+
max_rows_display: int = typer.Option(
82+
25, help=builtins._("Maximum number of rows to display. Use -1 for all rows.")
83+
),
84+
show_all_columns: bool = typer.Option(
85+
False, "--show-all-cols", help=builtins._("Show all columns in the output table.")
86+
)
87+
):
88+
# debug and verbose options are handled by the main callback
5389
if ctx.obj.get("DEBUG"):
5490
debug(f"query_exoplanet - planet_name: {planet_name}")
5591

@@ -70,4 +106,4 @@ def exoplanet_command(
70106
handle_astroquery_exception(ctx, e, _("NASA Exoplanet Archive query"))
71107
raise typer.Exit(code=1)
72108

73-
return exoplanet_command
109+
return exoplanet_app

astroquery_cli/modules/heasarc_cli.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def get_app():
3232
@global_keyboard_interrupt_handler
3333
def heasarc_callback(
3434
ctx: typer.Context,
35-
# Moved query options to callback
3635
ra: Optional[float] = typer.Option(None, help=builtins._("Right Ascension in degrees.")),
3736
dec: Optional[float] = typer.Option(None, help=builtins._("Declination in degrees.")),
3837
radius: Optional[float] = typer.Option(None, help=builtins._("Search radius in degrees (for cone search).")),
@@ -47,27 +46,50 @@ def heasarc_callback(
4746
max_rows: int = typer.Option(
4847
100, "--max-rows", help=builtins._("Maximum number of rows to retrieve from the HEASARC database. Use -1 for all rows.")
4948
),
50-
enable_debug: bool = typer.Option(
49+
enable_debug_flag: bool = typer.Option( # Renamed to avoid conflict
5150
False,
5251
"-t",
5352
"--debug",
54-
help=_("Enable debug mode with verbose output."),
53+
help=builtins._("Enable debug mode with verbose output."),
5554
envvar="AQC_DEBUG"
5655
),
57-
verbose: bool = typer.Option(
56+
enable_verbose_flag: bool = typer.Option( # Renamed to avoid conflict
5857
False,
5958
"-v",
6059
"--verbose",
61-
help=_("Enable verbose output.")
60+
help=builtins._("Enable verbose output.")
6261
),
6362
):
64-
setup_debug_context(ctx, enable_debug, verbose)
63+
setup_debug_context(ctx, enable_debug_flag, enable_verbose_flag) # Use new parameter names
6564
debug(f"heasarc_callback: ctx.invoked_subcommand={ctx.invoked_subcommand}, ctx.args={ctx.args}")
66-
heasarc = Heasarc() # Instantiate Heasarc here for all operations
6765

68-
# Check if a subcommand was invoked (e.g., 'list')
69-
if ctx.invoked_subcommand is not None:
70-
return # Let the subcommand handle its own logic
66+
# Check if a subcommand was invoked (e.g., 'list' or 'query')
67+
# If no subcommand and no explicit help flag, display only commands section
68+
if ctx.invoked_subcommand is None and \
69+
not any(arg in ["-h", "--help"] for arg in ctx.args):
70+
# Capture the full help output by explicitly calling the app with --help
71+
help_output_capture = StringIO()
72+
with redirect_stdout(help_output_capture):
73+
try:
74+
app(ctx.args + ["--help"])
75+
except SystemExit:
76+
pass
77+
full_help_text = help_output_capture.getvalue()
78+
79+
# Extract only the "Commands" section using regex, including the full bottom border
80+
commands_match = re.search(r'╭─ Commands ─.*?(\n(?:│.*?\n)*)╰─.*─╯', full_help_text, re.DOTALL)
81+
if commands_match:
82+
commands_section = commands_match.group(0)
83+
filtered_commands_section = "\n".join([
84+
line for line in commands_section.splitlines() if "Usage:" not in line
85+
])
86+
console.print(filtered_commands_section)
87+
else:
88+
console.print(full_help_text)
89+
raise typer.Exit()
90+
91+
# If a subcommand was invoked or -h/--help was provided, let Typer handle it.
92+
# The options defined in this callback will be part of the full help.
7193

7294
@app.command(name="query", help=builtins._("Query a specific HEASARC mission. Use 'heasarc list' to see available missions."))
7395
@global_keyboard_interrupt_handler

astroquery_cli/modules/nist_cli.py

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def get_app():
2323
_ = builtins._
2424
app = typer.Typer(
2525
name="nist",
26-
help="Query the NIST Atomic Spectra Database.",
26+
help=builtins._("Query the NIST Atomic Spectra Database."),
2727
invoke_without_command=True,
2828
no_args_is_help=False
2929
)
@@ -34,64 +34,84 @@ def nist_callback(
3434
ctx: typer.Context,
3535
query_string: Optional[str] = typer.Argument(
3636
None,
37-
help="Primary query input. Can be:\n"
38-
" 1. A wavelength range (e.g., '2000 3000').\n"
39-
" 2. A line name (e.g., 'Fe II', 'H I').\n"
40-
"If a line name is provided without explicit --minwav/--maxwav, a broad default range will be used."
37+
help=builtins._("Primary query input: wavelength range (e.g., '2000 3000') or line name (e.g., 'Fe II').")
4138
),
4239
minwav: Optional[float] = typer.Option(
4340
None,
44-
help="Explicit minimum wavelength (e.g., 2000). Overrides any wavelength range parsed from 'query_string'. "
45-
"Can be combined with '--linename'."
41+
help=builtins._("Explicit minimum wavelength (e.g., 2000). Overrides any wavelength range parsed from 'query_string'. "
42+
"Can be combined with '--linename'.")
4643
),
4744
maxwav: Optional[float] = typer.Option(
4845
None,
49-
help="Explicit maximum wavelength (e.g., 3000). Overrides any wavelength range parsed from 'query_string'. "
50-
"Can be combined with '--linename'."
46+
help=builtins._("Explicit maximum wavelength (e.g., 3000). Overrides any wavelength range parsed from 'query_string'. "
47+
"Can be combined with '--linename'.")
5148
),
5249
linename: Optional[str] = typer.Option(
5350
None,
54-
help="Explicit line name (e.g., 'Fe II', 'H I'). Overrides any line name parsed from 'query_string'. "
55-
"Can be combined with explicit '--minwav' and '--maxwav' for a specific range."
51+
help=builtins._("Explicit line name (e.g., 'Fe II', 'H I'). Overrides any line name parsed from 'query_string'. "
52+
"Can be combined with explicit '--minwav' and '--maxwav' for a specific range.")
5653
),
5754
output_file: Optional[str] = common_output_options["output_file"],
5855
output_format: Optional[str] = common_output_options["output_format"],
5956
max_rows_display: int = typer.Option(
60-
25, help="Maximum number of rows to display. Use -1 for all rows."
57+
25, help=builtins._("Maximum number of rows to display. Use -1 for all rows.")
6158
),
6259
show_all_columns: bool = typer.Option(
63-
False, "--show-all-cols", help="Show all columns in the output table."
60+
False, "--show-all-cols", help=builtins._("Show all columns in the output table.")
6461
),
65-
test: bool = typer.Option(False, "--test", "-t", help="Enable test mode and print elapsed time."),
62+
test: bool = typer.Option(False, "--test", "-t", help=builtins._("Enable test mode and print elapsed time.")),
6663
debug_flag: bool = typer.Option( # Renamed to avoid conflict with imported debug
6764
False,
6865
"--debug", # Removed -t to avoid conflict with --test
69-
help="Enable debug mode with verbose output.",
66+
help=builtins._("Enable debug mode with verbose output."),
7067
envvar="AQC_DEBUG"
7168
),
7269
verbose: bool = typer.Option(
7370
False,
7471
"-v",
7572
"--verbose",
76-
help="Enable verbose output."
73+
help=builtins._("Enable verbose output.")
7774
)
7875
):
7976
setup_debug_context(ctx, debug_flag, verbose)
8077

8178
# If no query string and no wavelength/linename options are provided, show help
8279
# This handles the case where `nist` is called without any arguments
8380
if query_string is None and minwav is None and maxwav is None and linename is None:
84-
# If help is not explicitly requested, but no arguments are given, show help
81+
help_output_capture = StringIO()
82+
with redirect_stdout(help_output_capture):
83+
try:
84+
app(["--help"]) # Call app with --help to get its own help
85+
except SystemExit:
86+
pass
87+
full_help_text = help_output_capture.getvalue()
88+
89+
# Description text
90+
desc_text = (
91+
_("Query the NIST Atomic Spectra Database. You can query by:\n") +
92+
_(" 1. Wavelength range: Provide two numbers (e.g., '2000 3000').\n") +
93+
_(" 2. Line name: Provide a line name (e.g., 'Fe II', 'H I').\n") +
94+
_(" If a line name is provided without explicit --minwav/--maxwav, a broad default range will be used.\n") +
95+
_("You can combine explicit --minwav/--maxwav with --linename for a specific range.\n")
96+
)
97+
98+
# If help is not explicitly requested, show simplified help
8599
if not any(arg in ["-h", "--help"] for arg in ctx.args):
86-
help_output_capture = StringIO()
87-
with redirect_stdout(help_output_capture):
88-
try:
89-
app(["--help"]) # Call app with --help to get its own help
90-
except SystemExit:
91-
pass
92-
console.print(help_output_capture.getvalue())
100+
console.print(desc_text)
101+
102+
# Extract and print Arguments section
103+
args_match = re.search(r"(╭─ Arguments ─+╮\n.*?\n╰─+╯)", full_help_text, flags=re.DOTALL)
104+
if args_match:
105+
console.print(args_match.group(0))
106+
107+
console.print(_("\nUse --help to see all available options."))
108+
raise typer.Exit()
109+
else:
110+
# If help IS explicitly requested, print full help
111+
console.print(desc_text)
112+
console.print(full_help_text)
93113
raise typer.Exit()
94-
# If help IS explicitly requested, Typer will handle it, so we just return.
114+
# Typer will handle explicit --help, so we just return if it's not handled above.
95115
return
96116

97117
_minwav = minwav
@@ -125,7 +145,7 @@ def nist_callback(
125145
_maxwav = 100000.0 # Default maximum wavelength
126146

127147
if _minwav is None or _maxwav is None:
128-
console.print(f"[red]{_('Error: Wavelength range (MINWAV and MAXWAV) must be provided either as arguments or implicitly via a line name query.')}[/red]")
148+
console.print(_(f"[red]Error: Wavelength range (MINWAV and MAXWAV) must be provided either as arguments or implicitly via a line name query.[/red]"))
129149
raise typer.Exit(code=1)
130150

131151
if ctx.obj.get("DEBUG"):

0 commit comments

Comments
 (0)