Skip to content

Commit c66d028

Browse files
committed
Added some basic user completion on Windows
1 parent 05d2d2b commit c66d028

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

cmd2.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,29 +1602,35 @@ def path_complete(self, text, line, begidx, endidx, dir_exe_only=False, dir_only
16021602

16031603
# Used to complete ~ and ~user strings with a list of users that have existing home dirs
16041604
def complete_users():
1605-
# Only works on Unix systems
1606-
try:
1607-
import pwd
1608-
except ImportError:
1609-
return []
16101605

1611-
# Get a list of users from password database
1606+
# We are returning ~user strings that resolve to directories, so don't append a space or quote
1607+
self.allow_appended_space = False
1608+
self.allow_closing_quote = False
1609+
16121610
users = []
1613-
for cur_pw in pwd.getpwall():
16141611

1615-
# Check if the user has an existing home dir
1616-
if os.path.isdir(cur_pw.pw_dir):
1612+
# Windows lacks the pwd module so we can't get a list of users.
1613+
# Instead we will add a slash once the user enters text that
1614+
# resolves to an existing home directory.
1615+
if sys.platform.startswith('win'):
1616+
expanded_path = os.path.expanduser(text)
1617+
if os.path.isdir(expanded_path):
1618+
users.append(text + os.path.sep)
1619+
else:
1620+
import pwd
16171621

1618-
# Add a ~ to the user to match against text
1619-
cur_user = '~' + cur_pw.pw_name
1620-
if cur_user.startswith(text):
1621-
if add_trailing_sep_if_dir:
1622-
cur_user += os.path.sep
1623-
users.append(cur_user)
1622+
# Iterate through a list of users from the password database
1623+
for cur_pw in pwd.getpwall():
16241624

1625-
# These are directories, so don't add a space or quote
1626-
self.allow_appended_space = False
1627-
self.allow_closing_quote = False
1625+
# Check if the user has an existing home dir
1626+
if os.path.isdir(cur_pw.pw_dir):
1627+
1628+
# Add a ~ to the user to match against text
1629+
cur_user = '~' + cur_pw.pw_name
1630+
if cur_user.startswith(text):
1631+
if add_trailing_sep_if_dir:
1632+
cur_user += os.path.sep
1633+
users.append(cur_user)
16281634

16291635
return users
16301636

tests/test_completion.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,8 @@ def test_path_completion_expand_user_dir(cmd2_app):
352352
begidx = endidx - len(text)
353353
completions = cmd2_app.path_complete(text, line, begidx, endidx)
354354

355-
# On Windows there should be no results, since it lacks the pwd module
356-
if sys.platform.startswith('win'):
357-
assert completions == []
358-
else:
359-
expected = text + os.path.sep
360-
assert expected in completions
355+
expected = text + os.path.sep
356+
assert expected in completions
361357

362358
def test_path_completion_user_expansion(cmd2_app):
363359
# Run path with a tilde and a slash

0 commit comments

Comments
 (0)