Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Padding of -help output with Arg (patch included) #8496

Closed
vicuna opened this issue Mar 1, 2004 · 3 comments
Closed

Padding of -help output with Arg (patch included) #8496

vicuna opened this issue Mar 1, 2004 · 3 comments

Comments

@vicuna
Copy link

vicuna commented Mar 1, 2004

Original bug ID: 2297
Reporter: administrator
Status: closed
Resolution: fixed
Priority: normal
Severity: feature
Category: ~DO NOT USE (was: OCaml general)

Bug description

Full_Name: Kenneth Knowles
Version: ocaml 3.07
OS: linux 2.6.3
Submission from: adsl-64-162-212-212.dsl.snfc21.pacbell.net (64.162.212.212)

I think, for aesthetic reasons, that it is nice to align the help strings when
-help is specified for Arg.parse. I realize this is nearly trivial, but here
I've got a patch that pads the -help output (I use my modified Arg for many
projects):

-c Compile file to opcodes.
-o Specify output file.
-stdout Dump output to STDOUT instead of creating filename

instead of
-c Compile file to opcodes.
-o Specify output file.
-stdout Dump output to STDOUT instead of creating filename

Here is the patch from diff -u (I hope it comes out cleanly through this web
interface):

kenn@tallman frontends $ diff -u ~/src/ocaml/cvs/ocaml/stdlib/arg.ml arg.ml
--- /home/kenn/src/ocaml/cvs/ocaml/stdlib/arg.ml 2004-02-29
23:10:01.926620136 -0800
+++ arg.ml 2004-02-29 23:00:07.336193020 -0800
@@ -63,20 +63,36 @@
| h::t -> (List.fold_left (fun x y -> x ^ sep ^ y) (prefix ^ h) t) ^ suffix
;;

-let print_spec buf (key, spec, doc) =
+let pad str len =

  •   if (String.length str) > len then
    
  •           str
    
  •   else
    
  •           String.concat "" [str; String.make (len - (String.length str)) '
    

']
+
+let print_spec ?(pad_to = 0) buf (key, spec, doc) =
match spec with

  • | Symbol (l, _) -> bprintf buf " %s %s %s\n" key (make_symlist "{" "|" "}"
    l)
  • | Symbol (l, _) -> bprintf buf " %s %s %s\n" (pad key pad_to) (make_symlist
    "{" "|" "}" l)
    doc
  • | _ -> bprintf buf " %s %s\n" key doc
  • | _ -> bprintf buf " %s %s\n" (pad key pad_to) doc
    ;;

+let rec maxkeylen ?(max = 0) li =

  •   match li with
    
  •   | [] -> max
    
  •   | (key, spec, doc) :: rest ->
    
  •           if (String.length key) > max then
    
  •                   maxkeylen ~max:(String.length key) rest
    
  •           else
    
  •                   maxkeylen ~max rest
    

let usage_b buf speclist errmsg =

  • let maxlen = maxkeylen speclist in
    bprintf buf "%s\n" errmsg;
  • List.iter (print_spec buf) speclist;
  • List.iter (print_spec ~pad_to:maxlen buf) speclist;
    try ignore (assoc3 "-help" speclist)
  • with Not_found -> bprintf buf " -help Display this list of options\n";
  • with Not_found -> bprintf buf " %s Display this list of options\n" (pad
    "-help" maxlen);
    try ignore (assoc3 "--help" speclist)
  • with Not_found -> bprintf buf " --help Display this list of options\n";
  • with Not_found -> bprintf buf " %s Display this list of options\n" (pad
    "--help" maxlen);
    ;;

let usage speclist errmsg =

@vicuna
Copy link
Author

vicuna commented Jun 11, 2004

Comment author: administrator

On Fri, Jun 11, 2004 at 04:47:48PM +0200, Damien Doligez wrote:

You can do it by indenting your source code carefully:

let argspec = [
"-c", Arg.Set opcode_flag,
" Compile file to opcodes";
"-o", Arg.String set_output_file,
" Set output file";
"-stdout", Arg.Unit set_stdout,
" Dump output to STDOUT instead of creating filename";
];;

Moreover, I don't like to do it automatically because this is what I
really want:

-c Compile file to opcodes.
-o Specify output file.
-stdout Dump output to STDOUT instead of creating filename

I agree completely, your solution is greater than my suggestion. Thanks for the
reply.

Kenn

@vicuna
Copy link
Author

vicuna commented Jun 11, 2004

Comment author: administrator

Full_Name: Kenneth Knowles
Version: ocaml 3.07

I think, for aesthetic reasons, that it is nice to align the help strings
when
-help is specified for Arg.parse. I realize this is nearly trivial, but here
I've got a patch that pads the -help output (I use my modified Arg for many
projects):

-c Compile file to opcodes.
-o Specify output file.
-stdout Dump output to STDOUT instead of creating filename

You can do it by indenting your source code carefully:

let argspec = [
"-c", Arg.Set opcode_flag,
" Compile file to opcodes";
"-o", Arg.String set_output_file,
" Set output file";
"-stdout", Arg.Unit set_stdout,
" Dump output to STDOUT instead of creating filename";
];;

Moreover, I don't like to do it automatically because this is what I
really want:

-c Compile file to opcodes.
-o Specify output file.
-stdout Dump output to STDOUT instead of creating filename

So what I'm going to do is expand the first space of the help
string instead of simply prepending spaces. And I will make it
optional in the form of a function:
Arg.align: (key * spec * doc) list -> (key * spec * doc) list

To get what you want, you will simply need to add a space at the
beginning of your doc strings, and call Arg.align on your argument
specification list.

-- Damien

@vicuna
Copy link
Author

vicuna commented Jun 11, 2004

Comment author: administrator

wish (more or less) granted DD 2004-06-11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant