12
12
from sanic .cli .arguments import Group
13
13
from sanic .cli .base import SanicArgumentParser , SanicHelpFormatter
14
14
from sanic .cli .console import SanicREPL
15
+ from sanic .cli .executor import Executor , make_executor_parser
15
16
from sanic .cli .inspector import make_inspector_parser
16
17
from sanic .cli .inspector_client import InspectorClient
17
18
from sanic .helpers import _default , is_atty
@@ -64,11 +65,11 @@ def __init__(self) -> None:
64
65
)
65
66
self .args : Namespace = Namespace ()
66
67
self .groups : List [Group ] = []
67
- self .inspecting = False
68
+ self .run_mode = "serve"
68
69
69
70
def attach (self ):
70
71
if len (sys .argv ) > 1 and sys .argv [1 ] == "inspect" :
71
- self .inspecting = True
72
+ self .run_mode = "inspect"
72
73
self .parser .description = get_logo (True )
73
74
make_inspector_parser (self .parser )
74
75
return
@@ -78,8 +79,13 @@ def attach(self):
78
79
instance .attach ()
79
80
self .groups .append (instance )
80
81
82
+ if len (sys .argv ) > 2 and sys .argv [2 ] == "exec" :
83
+ self .run_mode = "exec"
84
+ self .parser .description = get_logo (True )
85
+ make_executor_parser (self .parser )
86
+
81
87
def run (self , parse_args = None ):
82
- if self .inspecting :
88
+ if self .run_mode == "inspect" :
83
89
self ._inspector ()
84
90
return
85
91
@@ -92,13 +98,22 @@ def run(self, parse_args=None):
92
98
parse_args = ["--version" ]
93
99
94
100
if not legacy_version :
101
+ if self .run_mode == "exec" :
102
+ parse_args = [
103
+ a
104
+ for a in (parse_args or sys .argv [1 :])
105
+ if a not in "-h --help" .split ()
106
+ ]
95
107
parsed , unknown = self .parser .parse_known_args (args = parse_args )
96
108
if unknown and parsed .factory :
97
109
for arg in unknown :
98
110
if arg .startswith ("--" ):
99
111
self .parser .add_argument (arg .split ("=" )[0 ])
100
112
101
- self .args = self .parser .parse_args (args = parse_args )
113
+ if self .run_mode == "exec" :
114
+ self .args , _ = self .parser .parse_known_args (args = parse_args )
115
+ else :
116
+ self .args = self .parser .parse_args (args = parse_args )
102
117
self ._precheck ()
103
118
app_loader = AppLoader (
104
119
self .args .target , self .args .factory , self .args .simple , self .args
@@ -110,6 +125,12 @@ def run(self, parse_args=None):
110
125
except ValueError as e :
111
126
error_logger .exception (f"Failed to run app: { e } " )
112
127
else :
128
+ if self .run_mode == "exec" :
129
+ self ._executor (app , kwargs )
130
+ return
131
+ elif self .run_mode != "serve" :
132
+ raise ValueError (f"Unknown run mode: { self .run_mode } " )
133
+
113
134
if self .args .repl :
114
135
self ._repl (app )
115
136
for http_version in self .args .http :
@@ -152,6 +173,10 @@ def _inspector(self):
152
173
kwargs ["args" ] = positional [1 :]
153
174
InspectorClient (host , port , secure , raw , api_key ).do (action , ** kwargs )
154
175
176
+ def _executor (self , app : Sanic , kwargs : dict ):
177
+ args = sys .argv [3 :]
178
+ Executor (app , kwargs ).run (self .args .command , args )
179
+
155
180
def _repl (self , app : Sanic ):
156
181
if is_atty ():
157
182
0 commit comments