From 243f5a0dafdc69b84e20e83b82a9fc1d898ef6a3 Mon Sep 17 00:00:00 2001 From: Matteo Carnelos Date: Sat, 19 Apr 2025 19:19:00 +0200 Subject: [PATCH 1/2] Update `cot` CLI behavior --- CotEditor/Resources/cot | 56 +++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/CotEditor/Resources/cot b/CotEditor/Resources/cot index 73e8420305..4f10a5bc00 100755 --- a/CotEditor/Resources/cot +++ b/CotEditor/Resources/cot @@ -26,7 +26,6 @@ limitations under the License. """ import argparse -import errno import os import sys import time @@ -34,7 +33,7 @@ from subprocess import Popen, PIPE, CalledProcessError # meta data -__version__ = '5.1.0' +__version__ = '6.0.0' __description__ = 'command-line utility for CotEditor.' @@ -244,7 +243,7 @@ def parse_args(): type=str, metavar='FILE', nargs='*', # allow wildcard - help="path to file to open" + help="edit specified file(s)" ) # set optional arguments @@ -267,11 +266,6 @@ def parse_args(): default=False, help="open the document as read-only" ) - parser.add_argument('-n', '--new', - action='store_true', - default=False, - help="create a new blank document" - ) parser.add_argument('-s', '--syntax', type=str, help="set specific syntax to opened document" @@ -288,34 +282,30 @@ def parse_args(): args = parser.parse_args() # create a flag specifying if create a new blank window or file - args.new_window = args.new and not args.files + if not args.files: + args.new_window = True + return args + + if args.files == ['-']: + return args - # check file existence and create if needed - if args.files and args.files != ['-']: - # strip symlink - args.files = list(map(os.path.realpath, args.files)) + # strip symlink + args.files = list(map(os.path.realpath, args.files)) + + for path in args.files: # skip file check if file is directory - if not args.new and os.path.isdir(args.files[0]): - return args - - open_mode = 'r' - if args.new and not os.path.exists(args.files[0]): - open_mode = 'w' # overwrite mode to create new file - # create directory if not exists yet - filepath = args.files[0] - dirpath = os.path.dirname(filepath) - if dirpath: - try: - os.makedirs(dirpath) - except OSError as err: # guard against race condition - if err.errno != errno.EEXIST: - parser.error("argument FILE: {}".format(err)) + if os.path.isdir(path): + continue + # create directory if not exists yet + try: + os.makedirs(os.path.dirname(path), exist_ok=True) + except OSError as err: + parser.error("argument FILE: {}".format(err)) # check readability or create new one - for path in args.files: - try: - open(path, open_mode).close() - except IOError as err: - parser.error("argument FILE: {}".format(err)) + try: + open(path, 'a').close() + except IOError as err: + parser.error("argument FILE: {}".format(err)) return args From a4a423b92e05b10cd81ef6402612cddc465c8500 Mon Sep 17 00:00:00 2001 From: Matteo Carnelos Date: Sat, 19 Apr 2025 20:04:53 +0200 Subject: [PATCH 2/2] Remove the need of the `new_window` flag --- CotEditor/Resources/cot | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/CotEditor/Resources/cot b/CotEditor/Resources/cot index 4f10a5bc00..09259c6332 100755 --- a/CotEditor/Resources/cot +++ b/CotEditor/Resources/cot @@ -281,11 +281,6 @@ def parse_args(): args = parser.parse_args() - # create a flag specifying if create a new blank window or file - if not args.files: - args.new_window = True - return args - if args.files == ['-']: return args @@ -345,7 +340,7 @@ def main(args, stdin): app.tell_document('set range of selection to {0, 0}') document_count = 1 - elif args.new_window: + else: # new blank document app.tell('make new document') document_count = 1 @@ -373,7 +368,7 @@ def main(args, stdin): args.line or 1, args.column or 0)) # wait for window close - if args.wait and (len(args.files) == 1 or stdin or args.new_window): + if args.wait and (len(args.files) <= 1 or stdin): window_id = app.window_id() while app.is_running() and app.window_exists(window_id): time.sleep(WAIT_INTERVAL)