diff --git a/salt/utils/parsers.py b/salt/utils/parsers.py index add8e3f0a7..34eaba52a0 100644 --- a/salt/utils/parsers.py +++ b/salt/utils/parsers.py @@ -1108,41 +1108,44 @@ class SaltCMDOptionParser(OptionParser, ConfigDirMixIn, MergeConfigMixIn, else: self.config['tgt'] = self.args[0].split() else: - self.config['tgt'] = self.args[0] - + try: + self.config['tgt'] = self.args[0] + except IndexError: + self.exit(42, '\nCannot execute command without defining a target.\n\n') # Detect compound command and set up the data for it - if ',' in self.args[1]: - self.config['fun'] = self.args[1].split(',') - self.config['arg'] = [[]] - cmd_index = 0 - if (self.args[2:].count(self.options.args_separator) == - len(self.config['fun']) - 1): - # new style parsing: standalone argument separator - for arg in self.args[2:]: - if arg == self.options.args_separator: - cmd_index += 1 - self.config['arg'].append([]) - else: - self.config['arg'][cmd_index].append(arg) + if self.args: + if ',' in self.args[1]: + self.config['fun'] = self.args[1].split(',') + self.config['arg'] = [[]] + cmd_index = 0 + if (self.args[2:].count(self.options.args_separator) == + len(self.config['fun']) - 1): + # new style parsing: standalone argument separator + for arg in self.args[2:]: + if arg == self.options.args_separator: + cmd_index += 1 + self.config['arg'].append([]) + else: + self.config['arg'][cmd_index].append(arg) + else: + # old style parsing: argument separator can be inside args + for arg in self.args[2:]: + if self.options.args_separator in arg: + sub_args = arg.split(self.options.args_separator) + for sub_arg_index, sub_arg in enumerate(sub_args): + if sub_arg: + self.config['arg'][cmd_index].append(sub_arg) + if sub_arg_index != len(sub_args) - 1: + cmd_index += 1 + self.config['arg'].append([]) + else: + self.config['arg'][cmd_index].append(arg) + if len(self.config['fun']) != len(self.config['arg']): + self.exit(42, 'Cannot execute compound command without ' + 'defining all arguments.') else: - # old style parsing: argument separator can be inside args - for arg in self.args[2:]: - if self.options.args_separator in arg: - sub_args = arg.split(self.options.args_separator) - for sub_arg_index, sub_arg in enumerate(sub_args): - if sub_arg: - self.config['arg'][cmd_index].append(sub_arg) - if sub_arg_index != len(sub_args) - 1: - cmd_index += 1 - self.config['arg'].append([]) - else: - self.config['arg'][cmd_index].append(arg) - if len(self.config['fun']) != len(self.config['arg']): - self.exit(42, 'Cannot execute compound command without ' - 'defining all arguments.') - else: - self.config['fun'] = self.args[1] - self.config['arg'] = self.args[2:] + self.config['fun'] = self.args[1] + self.config['arg'] = self.args[2:] def setup_config(self): return config.client_config(self.get_config_file_path())