@@ -6,12 +6,15 @@ class Generate < Base
66 help 'Generate the bash completion script to file or stdout'
77
88 usage 'completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]'
9+ usage 'completely generate [CONFIG_PATH --install PROGRAM --function NAME]'
910 usage 'completely generate (-h|--help)'
1011
1112 option_function
1213 option '-w --wrap NAME' , 'Wrap the completion script inside a function that echos the ' \
1314 'script. This is useful if you wish to embed it directly in your script.'
1415
16+ option '-i --install PROGRAM' , 'Install the generated script as completions for PROGRAM.'
17+
1518 param 'CONFIG_PATH' , <<~USAGE
1619 Path to the YAML configuration file [default: completely.yaml].
1720 Use '-' to read from stdin.
@@ -35,17 +38,34 @@ class Generate < Base
3538 def run
3639 wrap = args [ '--wrap' ]
3740 output = wrap ? wrapper_function ( wrap ) : script
41+
42+ if args [ '--install' ]
43+ install output
44+ else
45+ show_or_save output
46+ end
47+ end
48+
49+ private
50+
51+ def install ( content )
52+ installer = Installer . from_string program : args [ '--install' ] , string : content
53+ success = installer . install force : true
54+ raise InstallError , "Failed running command:\n nb`#{ installer . install_command_string } `" unless success
55+ say "Saved m`#{ installer . target_path } `"
56+ say 'You may need to restart your session to test it'
57+ end
58+
59+ def show_or_save ( content )
3860 if output_path == '-'
39- puts output
61+ puts content
4062 else
41- File . write output_path , output
63+ File . write output_path , content
4264 say "Saved m`#{ output_path } `"
4365 end
4466 syntax_warning unless completions . valid?
4567 end
4668
47- private
48-
4969 def wrapper_function ( wrapper_name )
5070 completions . wrapper_function wrapper_name
5171 end
0 commit comments