in generate_commands_from_synopsis.py [0:0]
def generate_single_command(results, avg_options=3, idx=None):
if idx is None:
idx = np.random.randint(len(results))
row = results.iloc[idx]
options = row['options']
cmd = [row['cmd']]
text = [row['synopsis']]
if options:
p = np.random.rand(len(options))
if len(options) > avg_options:
p = p < avg_options / len(options)
else:
p = p < 0.3
p_options = [x for x, pp in zip(options, p) if pp]
for x in p_options:
if '--help' in x['long'] or '--version' in x['long'] or '-help' in x['short']:
if len(p_options) > 1:
continue
option_variants = x['short'] + x['long']
option_var = np.random.choice(option_variants)
current_option = [option_var]
if x['expectsarg']:
current_option.append('ARG')
add_text = " ".join(x['text'].lower().split()[:5])
text.append(add_text)
cmd.append(tuple(current_option))
cmd[1:] = sorted(cmd[1:], key=lambda x: "2"+x[0][2:] if x[0].startswith('--') else "1"+x[0][1:])
real_cmd = [cmd[0]]
for x in cmd[1:]:
real_cmd.extend(x)
cmd = real_cmd
cmd.extend(["ARG"] * row['required'])
return " ".join(cmd), " ".join(text)