Skip to content

Commit 7ed4129

Browse files
committed
!squash
1 parent 886bba9 commit 7ed4129

File tree

2 files changed

+161
-71
lines changed

2 files changed

+161
-71
lines changed

src/tmuxp/cli/__init__.py

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
~~~~~~~~~
55
66
"""
7+
import argparse
78
import logging
89
import os
910
import sys
1011

11-
import click
12-
1312
from libtmux.__about__ import __version__ as libtmux_version
1413
from libtmux.common import has_minimum_version
1514
from libtmux.exc import TmuxCommandNotFound
@@ -18,36 +17,60 @@
1817
from .. import exc
1918
from ..__about__ import __version__
2019
from ..log import setup_logger
21-
from .convert import command_convert
22-
from .debug_info import command_debug_info
23-
from .edit import command_edit
24-
from .freeze import command_freeze
25-
from .import_config import command_import
26-
from .load import command_load
27-
from .shell import command_shell
20+
21+
# from .convert import command_convert, create_convert_subparser
22+
# from .debug_info import command_debug_info, create_debug_info_subparser
23+
# from .edit import command_edit, create_edit_subparser
24+
# from .freeze import command_freeze, create_command_freeze
25+
# from .import_config import command_import, create_command_import
26+
from .load import command_load, create_load_subparser
27+
28+
# from .shell import command_shell, create_command_shell
2829
from .utils import tmuxp_echo
2930

3031
logger = logging.getLogger(__name__)
3132

3233

33-
@click.group(context_settings={"obj": {}, "help_option_names": ["-h", "--help"]})
34-
@click.version_option(
35-
__version__,
36-
"-V",
37-
"--version",
38-
message=f"%(prog)s %(version)s, libtmux {libtmux_version}",
39-
)
40-
@click.option(
41-
"--log-level",
42-
default="INFO",
43-
help="Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)",
44-
)
45-
def cli(log_level):
34+
def create_parser() -> argparse.ArgumentParser:
35+
parser = argparse.ArgumentParser(prog="tmuxp")
36+
parser.add_argument(
37+
"--version",
38+
"-V",
39+
action="version",
40+
version=f"%(prog)s {__version__}, libtmux {libtmux_version}",
41+
)
42+
parser.add_argument(
43+
"--log-level",
44+
action="store",
45+
default="INFO",
46+
help="Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)",
47+
)
48+
subparsers = parser.add_subparsers(dest="subparser_name")
49+
sync_parser = subparsers.add_parser("load", help="Load tmuxp workspaces")
50+
create_load_subparser(sync_parser)
51+
52+
return parser
53+
54+
55+
# @click.group(context_settings={"obj": {}, "help_option_names": ["-h", "--help"]})
56+
# @click.version_option(
57+
# __version__,
58+
# "-V",
59+
# "--version",
60+
# message=f"%(prog)s %(version)s, libtmux {libtmux_version}",
61+
# )
62+
# @click.option(
63+
# "--log-level",
64+
# default="INFO",
65+
# help="Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)",
66+
# )
67+
def cli(args=None):
4668
"""Manage tmux sessions.
4769
4870
Pass the "--help" argument to any command to see detailed help.
4971
See detailed documentation and examples at:
5072
http://tmuxp.git-pull.com/"""
73+
5174
try:
5275
has_minimum_version()
5376
except TmuxCommandNotFound:
@@ -56,7 +79,22 @@ def cli(log_level):
5679
except exc.TmuxpException as e:
5780
tmuxp_echo(e, err=True)
5881
sys.exit()
59-
setup_logger(logger=logger, level=log_level.upper())
82+
83+
parser = create_parser()
84+
args = parser.parse_args(args)
85+
86+
setup_logger(logger=logger, level=args.log_level.upper())
87+
88+
if args.subparser_name is None:
89+
parser.print_help()
90+
return
91+
elif args.subparser_name == "sync":
92+
sync(
93+
repo_terms=args.repo_terms,
94+
config=args.config,
95+
exit_on_error=args.exit_on_error,
96+
parser=parser,
97+
)
6098

6199

62100
def startup(config_dir):
@@ -72,12 +110,12 @@ def startup(config_dir):
72110
os.makedirs(config_dir)
73111

74112

75-
# Register sub-commands here
76-
cli.add_command(command_convert)
77-
cli.add_command(command_edit)
78-
cli.add_command(command_debug_info)
79-
cli.add_command(command_load)
80-
cli.add_command(command_ls)
81-
cli.add_command(command_freeze)
82-
cli.add_command(command_shell)
83-
cli.add_command(command_import)
113+
# # Register sub-commands here
114+
# cli.add_command(command_convert)
115+
# cli.add_command(command_edit)
116+
# cli.add_command(command_debug_info)
117+
# cli.add_command(command_load)
118+
# cli.add_command(command_ls)
119+
# cli.add_command(command_freeze)
120+
# cli.add_command(command_shell)
121+
# cli.add_command(command_import)

src/tmuxp/cli/load.py

Lines changed: 91 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Command line tool for managing tmux workspaces and tmuxp configurations.
22
3-
tmuxp.cli
4-
~~~~~~~~~
3+
tmuxp.cli.load
4+
~~~~~~~~~~~~~~
55
66
"""
7+
import argparse
78
import importlib
89
import logging
910
import os
@@ -20,7 +21,7 @@
2021

2122
from .. import config, exc, log, util
2223
from ..workspacebuilder import WorkspaceBuilder
23-
from .utils import ConfigPath, _validate_choices, get_config_dir, tmuxp_echo
24+
from .utils import _validate_choices, get_config_dir, tmuxp_echo
2425

2526

2627
def set_layout_hook(session, hook_name):
@@ -463,42 +464,93 @@ def config_file_completion(ctx, params, incomplete):
463464
return sorted(str(c) for c in choices if str(c).startswith(incomplete))
464465

465466

466-
@click.command(name="load", short_help="Load tmuxp workspaces.")
467-
@click.pass_context
468-
@click.argument(
469-
"config",
470-
type=ConfigPath(exists=True),
471-
nargs=-1,
472-
shell_complete=config_file_completion,
473-
)
474-
@click.option("-S", "socket_path", help="pass-through for tmux -S")
475-
@click.option("-L", "socket_name", help="pass-through for tmux -L")
476-
@click.option("-f", "tmux_config_file", help="pass-through for tmux -f")
477-
@click.option("-s", "new_session_name", help="start new session with new session name")
478-
@click.option("--yes", "-y", "answer_yes", help="yes", is_flag=True)
479-
@click.option(
480-
"-d", "detached", help="Load the session without attaching it", is_flag=True
481-
)
482-
@click.option(
483-
"-a",
484-
"append",
485-
help="Load configuration, appending windows to the current session",
486-
is_flag=True,
487-
)
488-
@click.option(
489-
"colors",
490-
"-2",
491-
flag_value=256,
492-
default=True,
493-
help="Force tmux to assume the terminal supports 256 colours.",
494-
)
495-
@click.option(
496-
"colors",
497-
"-8",
498-
flag_value=88,
499-
help="Like -2, but indicates that the terminal supports 88 colours.",
500-
)
501-
@click.option("--log-file", help="File to log errors/output to")
467+
def create_load_subparser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
468+
parser.add_argument(
469+
"config",
470+
nargs=-1,
471+
help="Filepath to session or filename of session if in tmuxp config directory",
472+
)
473+
parser.add_argument(
474+
"-L",
475+
dest="socket_name",
476+
metavar="socket_name",
477+
action="store",
478+
help="passthru to tmux(1) -L",
479+
)
480+
parser.add_argument(
481+
"-S",
482+
dest="socket_path",
483+
metavar="socket_path",
484+
action="store",
485+
help="passthru to tmux(1) -S",
486+
)
487+
488+
parser.add_argument(
489+
"-f",
490+
dest="tmux_config_file",
491+
metavar="tmux_config_file",
492+
help="passthru to tmux(1) -f",
493+
)
494+
parser.add_argument(
495+
"-s",
496+
dest="new_session_name",
497+
metavar="new_session_name",
498+
help="start new session with new session name",
499+
)
500+
parser.add_argument(
501+
"--yes",
502+
"-y",
503+
dest="answer_yes",
504+
action="store_true",
505+
help="always answer yes",
506+
)
507+
parser.add_argument(
508+
"-d",
509+
dest="detached",
510+
action="store_true",
511+
help="load the session without attaching it",
512+
)
513+
parser.add_argument(
514+
"-a",
515+
dest="append",
516+
action="store_true",
517+
help="load configuration, appending windows to the current session",
518+
)
519+
colorsgroup = parser.add_mutually_exclusive_group()
520+
521+
colorsgroup.add_argument(
522+
"-2",
523+
dest="colors",
524+
action="store_const",
525+
const=256,
526+
help="force tmux to assume the terminal supports 256 colours.",
527+
)
528+
529+
colorsgroup.add_argument(
530+
"-8",
531+
dest="colors",
532+
action="store_const",
533+
const=88,
534+
help="like -2, but indicates that the terminal supports 88 colours.",
535+
)
536+
537+
parser.set_defaults(colors=None)
538+
parser.add_argument(
539+
"--log-file",
540+
metavar="file_path",
541+
action="store",
542+
help="File to log errors/output to",
543+
)
544+
# parser.add_argument(
545+
# "--exit-on-error",
546+
# "-x",
547+
# action="store_true",
548+
# dest="exit_on_error",
549+
# help="Specify config",
550+
# )
551+
return parser
552+
553+
502554
def command_load(
503555
ctx,
504556
config,

0 commit comments

Comments
 (0)