Skip to content

Commit

Permalink
Improve bash completion
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusstefan committed Dec 17, 2024
1 parent 64645c5 commit afe28ee
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
22 changes: 14 additions & 8 deletions opensips/event/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
parser.add_argument('-bc', '--bash-complete',
type=str,
nargs='?',
const='',
const='events',
help='Provide options for bash completion')

event = parser.add_argument_group('event')
Expand Down Expand Up @@ -116,32 +116,38 @@ def main():
sys.exit(1)

if args.bash_complete is not None:
if args.bash_complete != '':
if len(args.bash_complete) > 1:
last_arg = '--' + args.bash_complete
else:
last_arg = '-' + args.bash_complete
if args.bash_complete not in ['params', 'events']:
last_arg = '--' + args.bash_complete if len(args.bash_complete) > 1 else '-' + args.bash_complete

for action in parser._actions:
if last_arg in action.option_strings:
if action.choices:
print(' '.join(action.choices))
break
sys.exit(0)
else:

if args.bash_complete == 'params':
options = []
for action in parser._actions:
for opt in action.option_strings:
options.append(opt)
print(' '.join(options))
sys.exit(0)

# if args.bash_complete == 'events':
try:
response = mi.execute('events_list', [])
events = response.get("Events", [])
event_names = [event["name"] for event in events]
print(' '.join(event_names))
sys.exit(0)
except Exception as e:
sys.exit(1)
options = []
for action in parser._actions:
for opt in action.option_strings:
options.append(opt)
print(' '.join(options))
sys.exit(0)

if args.event is None:
print(f'ERROR: unknown type: {args.type}')
Expand Down
22 changes: 14 additions & 8 deletions opensips/mi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
group.add_argument('-bc', '--bash-complete',
type=str,
nargs='?',
const='',
const='commands',
help='Provide options for bash completion')

group = parser.add_mutually_exclusive_group(required=False)
Expand Down Expand Up @@ -109,30 +109,36 @@ def main():


if args.bash_complete is not None:
if args.bash_complete != '':
if len(args.bash_complete) > 1:
last_arg = '--' + args.bash_complete
else:
last_arg = '-' + args.bash_complete
if args.bash_complete not in ['params', 'commands']:
last_arg = '--' + args.bash_complete if len(args.bash_complete) > 1 else '-' + args.bash_complete

for action in parser._actions:
if last_arg in action.option_strings:
if action.choices:
print(' '.join(action.choices))
break
sys.exit(0)
else:

if args.bash_complete == 'params':
options = []
for action in parser._actions:
for opt in action.option_strings:
options.append(opt)
print(' '.join(options))
sys.exit(0)

# if args.bash_complete == 'commands':
try:
response = mi.execute('which', [])
print(" ".join(response))
sys.exit(0)
except Exception as e:
sys.exit(1)
options = []
for action in parser._actions:
for opt in action.option_strings:
options.append(opt)
print(' '.join(options))
sys.exit(0)

if args.stats:
args.command = 'get_statistics'
Expand Down
7 changes: 6 additions & 1 deletion utils/completion/python-opensips
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ function _opensips-complete() {
completed_args="$completed_args $prev"
fi
fi
opts="$($1 $completed_args -bc)"

if [[ "${cur:0:1}" == "-" ]]; then
opts="$($1 $completed_args -bc params)"
else
opts="$($1 $completed_args -bc)"
fi
else
while [[ "${prev:0:1}" == "-" ]]; do
prev="${prev:1}"
Expand Down

0 comments on commit afe28ee

Please sign in to comment.