|
| 1 | +--[[ |
| 2 | + Parse given command line and return three values: |
| 3 | +
|
| 4 | + <f_in_name> <f_out_name> <formatter_options> |
| 5 | +
|
| 6 | + Alternatively may print usage help and return nothing. |
| 7 | +]] |
| 8 | + |
| 9 | +local usage_text = request('usage_text') |
| 10 | + |
| 11 | +local cmdline_processor = |
| 12 | + new(request('!.mechs.command_line_processor.interface')) |
| 13 | +cmdline_processor.allowed_params = |
| 14 | + { |
| 15 | + {name = 'f_in_name', type = 'string'}, |
| 16 | + {name = 'f_out_name', type = 'string'}, |
| 17 | + {name = 'right-margin', type = 'key_int'}, |
| 18 | + {name = 'max-text-width', type = 'key_int'}, |
| 19 | + {name = 'keep-comments', type = 'flag'}, |
| 20 | + {name = 'indent', type = 'key_str'}, |
| 21 | + } |
| 22 | + |
| 23 | +--[[ |
| 24 | + Although original default parameters are stored in |
| 25 | + [lua.save.formatter.interface] I override it here. |
| 26 | +]] |
| 27 | +local default_formatter_options = |
| 28 | + { |
| 29 | + right_margin = 120, |
| 30 | + max_text_width = 100, |
| 31 | + indent_chunk = ' ', |
| 32 | + keep_comments = true, |
| 33 | + } |
| 34 | + |
| 35 | +local table_to_str = request('!.formats.lua_table.save') |
| 36 | + |
| 37 | +return |
| 38 | + function(args) |
| 39 | + assert_table(args) |
| 40 | + if not args[1] or (args[1] == '--help') then |
| 41 | + print(usage_text) |
| 42 | + print('Default options:') |
| 43 | + print(table_to_str(default_formatter_options)) |
| 44 | + return |
| 45 | + end |
| 46 | + |
| 47 | + local params = cmdline_processor:run(args) |
| 48 | + |
| 49 | + local f_in_name, f_out_name |
| 50 | + f_in_name = params.f_in_name |
| 51 | + if not f_in_name then |
| 52 | + print(usage_text) |
| 53 | + return |
| 54 | + end |
| 55 | + f_out_name = params.f_out_name or (f_in_name .. '.formatted') |
| 56 | + |
| 57 | + params['max-text-width'] = params['max-text-width'] or params['right-margin'] |
| 58 | + params['right-margin'] = params['right-margin'] or params['max-text-width'] |
| 59 | + |
| 60 | + local formatter_options = |
| 61 | + new( |
| 62 | + default_formatter_options, |
| 63 | + { |
| 64 | + indent_chunk = params.indent, |
| 65 | + right_margin = params['right-margin'], |
| 66 | + max_text_width = params['max-text-width'], |
| 67 | + keep_comments = params['keep-comments'], |
| 68 | + } |
| 69 | + ) |
| 70 | + |
| 71 | + return f_in_name, f_out_name, formatter_options |
| 72 | + end |
0 commit comments