Skip to content

Update cot CLI behavior #1842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 21 additions & 36 deletions CotEditor/Resources/cot
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ limitations under the License.
"""

import argparse
import errno
import os
import sys
import time
from subprocess import Popen, PIPE, CalledProcessError


# meta data
__version__ = '5.1.0'
__version__ = '6.0.0'
__description__ = 'command-line utility for CotEditor.'


Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -287,35 +281,26 @@ 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 args.files == ['-']:
return args

# strip symlink
args.files = list(map(os.path.realpath, args.files))

# check file existence and create if needed
if args.files and 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

Expand Down Expand Up @@ -355,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
Expand Down Expand Up @@ -383,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)
Expand Down