6363 type = str ,
6464 help = 'command' )
6565
66+ group .add_argument ('-bc' , '--bash-complete' ,
67+ type = str ,
68+ nargs = '?' ,
69+ const = '' ,
70+ help = 'Provide options for bash completion' )
71+
6672group = parser .add_mutually_exclusive_group (required = False )
6773
6874group .add_argument ('-j' , '--json' ,
@@ -80,6 +86,54 @@ def main():
8086 """ Main function of the opensips-mi script """
8187 args = parser .parse_args ()
8288
89+ if args .type == 'fifo' :
90+ fifo_args = {}
91+ if args .fifo_file :
92+ fifo_args ['fifo_file' ] = args .fifo_file
93+ if args .fifo_fallback :
94+ fifo_args ['fifo_file_fallback' ] = args .fifo_fallback
95+ if args .fifo_reply_dir :
96+ fifo_args ['fifo_reply_dir' ] = args .fifo_reply_dir
97+ mi = OpenSIPSMI ('fifo' , ** fifo_args )
98+ elif args .type == 'http' :
99+ mi = OpenSIPSMI ('http' , url = f'http://{ args .ip } :{ args .port } /mi' )
100+ elif args .type == 'datagram' :
101+ mi = OpenSIPSMI ('datagram' ,
102+ datagram_ip = args .ip ,
103+ datagram_port = args .port ,
104+ timeout = 0.1 )
105+ else :
106+ if not args .bash_complete :
107+ print (f'Unknown type: { args .type } ' )
108+ sys .exit (1 )
109+
110+
111+ if args .bash_complete is not None :
112+ if args .bash_complete != '' :
113+ if len (args .bash_complete ) > 1 :
114+ last_arg = '--' + args .bash_complete
115+ else :
116+ last_arg = '-' + args .bash_complete
117+
118+ for action in parser ._actions :
119+ if last_arg in action .option_strings :
120+ if action .choices :
121+ print (' ' .join (action .choices ))
122+ break
123+ sys .exit (0 )
124+ else :
125+ options = []
126+ for action in parser ._actions :
127+ for opt in action .option_strings :
128+ options .append (opt )
129+ print (' ' .join (options ))
130+ try :
131+ response = mi .execute ('which' , [])
132+ print (" " .join (response ))
133+ sys .exit (0 )
134+ except Exception as e :
135+ sys .exit (1 )
136+
83137 if args .stats :
84138 print ('Using get_statistics! Be careful not to use '
85139 'command after -s/--stats.' )
@@ -99,25 +153,6 @@ def main():
99153 print ('Invalid JSON: ' , e )
100154 sys .exit (1 )
101155
102- if args .type == 'fifo' :
103- fifo_args = {}
104- if args .fifo_file :
105- fifo_args ['fifo_file' ] = args .fifo_file
106- if args .fifo_fallback :
107- fifo_args ['fifo_file_fallback' ] = args .fifo_fallback
108- if args .fifo_reply_dir :
109- fifo_args ['fifo_reply_dir' ] = args .fifo_reply_dir
110- mi = OpenSIPSMI ('fifo' , ** fifo_args )
111- elif args .type == 'http' :
112- mi = OpenSIPSMI ('http' , url = f'http://{ args .ip } :{ args .port } /mi' )
113- elif args .type == 'datagram' :
114- mi = OpenSIPSMI ('datagram' ,
115- datagram_ip = args .ip ,
116- datagram_port = args .port )
117- else :
118- print (f'Unknownt type: { args .type } ' )
119- sys .exit (1 )
120-
121156 try :
122157 response = mi .execute (args .command , args .parameters )
123158 print (json .dumps (response , indent = 4 ))
0 commit comments