Skip to content

Commit 441e1aa

Browse files
committed
tmuxp load <config> and tmuxp load -l and --list are treated correctly and exclusively
1 parent a675922 commit 441e1aa

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

tmuxp/cli.py

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def build_workspace(config_file, args):
138138

139139

140140
def subcommand_load(args):
141-
if args.list_configs:
141+
if args.list:
142142
startup(config_dir)
143143
configs_in_user = config.in_dir(config_dir)
144144
configs_in_cwd = config.in_cwd()
@@ -159,15 +159,15 @@ def subcommand_load(args):
159159

160160
print(output)
161161

162-
elif args.configs:
163-
if '.' in args.configs:
164-
args.configs.remove('.')
162+
elif args.config:
163+
if '.' in args.config:
164+
args.config.remove('.')
165165
if config.in_cwd():
166-
args.configs.append(config.in_cwd()[0])
166+
args.config.append(config.in_cwd()[0])
167167
else:
168168
print('No tmuxp configs found in current directory.')
169169

170-
for configfile in args.configs:
170+
for configfile in args.config:
171171
file_user = os.path.join(config_dir, configfile)
172172
file_cwd = os.path.join(cwd_dir, configfile)
173173
if os.path.exists(file_cwd) and os.path.isfile(file_cwd):
@@ -187,16 +187,16 @@ def subcommand_import_tmuxinator(args):
187187

188188

189189
def subcommand_convert(args):
190-
if args.configs:
191-
if '.' in args.configs:
192-
args.configs.remove('.')
190+
if args.config:
191+
if '.' in args.config:
192+
args.config.remove('.')
193193
if config.in_cwd():
194-
args.configs.append(config.in_cwd()[0])
194+
args.config.append(config.in_cwd()[0])
195195
else:
196196
print('No tmuxp configs found in current directory.')
197197

198198
try:
199-
configfile = args.configs[0]
199+
configfile = args.config
200200
except Exception:
201201
print('Please enter a config')
202202

@@ -242,17 +242,16 @@ def subcommand_convert(args):
242242

243243
def subcommand_attach_session(args):
244244
commands = []
245-
try:
246-
ctext = args.session_name[0]
247-
except IndexError as e:
248-
return
245+
ctext = args.session_name
249246

250-
t = Server()
247+
t = Server(
248+
socket_name=args.socket_name or None,
249+
socket_path=args.socket_path or None
250+
)
251251
try:
252-
session = [s for s in t.sessions if s.get('session_name') == ctext][0]
253-
except IndexError as e:
254-
print('Session not found.')
255-
return
252+
session = next((s for s in t.sessions if s.get('session_name') == ctext), None)
253+
if not session:
254+
raise Exception('Session not found.')
256255
except Exception as e:
257256
print(e.message[0])
258257
return
@@ -268,20 +267,25 @@ def subcommand_attach_session(args):
268267

269268
def subcommand_kill_session(args):
270269
commands = []
271-
ctext = args.session_name[0]
270+
ctext = args.session_name
271+
272+
t = Server(
273+
socket_name=args.socket_name or None,
274+
socket_path=args.socket_path or None
275+
)
272276

273-
t = Server()
274277
try:
275-
sessions = [s for s in t.sessions if s.get('session_name') == ctext]
278+
session = next((s for s in t.sessions if s.get('session_name') == ctext), None)
279+
if not session:
280+
raise Exception('Session not found.')
276281
except Exception as e:
277282
print(e.message[0])
278283
return
279284

280-
if (len(sessions) == 1):
281-
try:
282-
sessions[0].kill_session()
283-
except Exception as e:
284-
logger.error(e)
285+
try:
286+
session.kill_session()
287+
except Exception as e:
288+
logger.error(e)
285289

286290

287291
def cli_parser():
@@ -301,7 +305,6 @@ def cli_parser():
301305

302306
kill_session.add_argument(
303307
dest='session_name',
304-
nargs=1,
305308
type=str,
306309
default=None,
307310
)
@@ -311,22 +314,20 @@ def cli_parser():
311314

312315
attach_session.add_argument(
313316
dest='session_name',
314-
nargs=1,
315317
type=str,
316-
default=None,
317318
)
318319

319320
load = subparsers.add_parser('load')
320321

321-
load.add_argument(
322-
'-l', '--list', dest='list_configs', action='store_true',
322+
loadgroup = load.add_mutually_exclusive_group(required=True)
323+
loadgroup.add_argument(
324+
'-l', '--list', dest='list', action='store_true',
323325
help='List config files available')
324326

325-
load.add_argument(
326-
dest='configs',
327-
nargs=1,
327+
loadgroup.add_argument(
328+
dest='config',
328329
type=str,
329-
default=None,
330+
nargs='?',
330331
help='''\
331332
List of config files to launch session from.
332333
@@ -343,8 +344,7 @@ def cli_parser():
343344
convert = subparsers.add_parser('convert')
344345

345346
convert.add_argument(
346-
dest='configs',
347-
nargs=1,
347+
dest='config',
348348
type=str,
349349
default=None,
350350
help='''\
@@ -367,7 +367,6 @@ def cli_parser():
367367

368368
import_teamocil.add_argument(
369369
dest='config',
370-
nargs=1,
371370
type=str,
372371
default=None,
373372
help='''\
@@ -380,7 +379,6 @@ def cli_parser():
380379

381380
import_tmuxinator.add_argument(
382381
dest='config',
383-
nargs=1,
384382
type=str,
385383
default=None,
386384
help='''\
@@ -428,6 +426,10 @@ def main():
428426
subcommand_load(args)
429427
elif args.callback is subcommand_convert:
430428
subcommand_convert(args)
429+
elif args.callback is subcommand_import_teamocil:
430+
subcommand_import_teamocil(args)
431+
elif args.callback is subcommand_import_tmuxinator:
432+
subcommand_import_tmuxinator(args)
431433
elif args.callback is subcommand_attach_session:
432434
subcommand_attach_session(args)
433435
elif args.callback is subcommand_kill_session:

0 commit comments

Comments
 (0)