Skip to content

Commit 15d16be

Browse files
committed
Use newer command-line-parser features
Less parsing by hand, more done by the command-line-parser library. Note that there is one thing that could be considered a regression here: the --help output isn't quite as nice because the auto-generated output isn't wrapped nicely the way it was when done by hand. That will be fixed with dylan-lang/command-line-parser#51.
1 parent c479de3 commit 15d16be

File tree

1 file changed

+43
-80
lines changed

1 file changed

+43
-80
lines changed

melange/interface.dylan

Lines changed: 43 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -624,25 +624,6 @@ define method show-copyright (stream :: <stream>) => ()
624624
format(stream, "Copyright 2005-2015 Dylan Hackers\n");
625625
end method show-copyright;
626626

627-
define method show-usage (stream :: <stream>) => ()
628-
format(stream,
629-
"Usage: melange [-v] [--headers]\n"
630-
" [--Ttarget]\n"
631-
" [-Ddef[=val]...] [-Uundef...]\n"
632-
" [-Iincdir...] [--framework name...]\n"
633-
" [-m modulefile] infile [outfile]\n"
634-
" melange --defines\n"
635-
" melange --undefines\n"
636-
" melange --includes\n"
637-
" melange --help\n"
638-
" melange --version\n");
639-
end method show-usage;
640-
641-
define method show-usage-and-exit () => ()
642-
show-usage(*standard-error*);
643-
exit-application(1);
644-
end method show-usage-and-exit;
645-
646627
define method show-default-defines (stream :: <stream>) => ()
647628
for (i from 0 below $default-defines.size by 2)
648629
let name = $default-defines[i];
@@ -678,41 +659,6 @@ define method show-default-includes (stream :: <stream>) => ()
678659
end for;
679660
end method show-default-includes;
680661

681-
define method show-help (stream :: <stream>) => ()
682-
show-copyright(stream);
683-
format(stream, "\n");
684-
show-usage(stream);
685-
format(stream, "\n"
686-
"Options:\n"
687-
" -v, --verbose Print progress messages while parsing.\n"
688-
" (Includes --headers.)\n"
689-
" --headers Print each header file included while parsing.\n"
690-
" -T, --target Generate output for use only with the named target.\n"
691-
" Target can be one of: c-ffi, mindy. Defaults to c-ffi.\n"
692-
" -D, --define Define a C preprocessor macro for use by C headers.\n"
693-
" If no value is given, defaults to 1.\n"
694-
" -U, --undefine Prevent definition of a default preprocessor macro.\n"
695-
" (Use --defines to see the defaults. Use --undefines to"
696-
" see the default undefines.)\n"
697-
" -I, --includedir Extra directories to search for C headers.\n"
698-
" --framework The name of a framework bundle to search for C headers\n"
699-
" and child frameworks. Required when a child framework\n"
700-
" is directly referred to in an interface definition\n"
701-
" with no previous references to its parent; once a\n"
702-
" parent is seen,either via this option or in a clause\n"
703-
" of the interface definition, its children will be\n"
704-
" found automatically. (Note: Parent --framework\n"
705-
" options must be given before child framework options.)\n"
706-
" -m, --module-file Create a Dylan interchange file with a module\n"
707-
" definition that exports the interface names.\n"
708-
" --defines Show the default C preprocessor definitions.\n"
709-
" --undefines Show the default platform undefinitions.\n"
710-
" --includes Show the default C preprocessor include directories.\n"
711-
" --help Show this help text.\n"
712-
" --version Show version number.\n"
713-
);
714-
end method show-help;
715-
716662

717663
//----------------------------------------------------------------------
718664
// The main program
@@ -725,55 +671,85 @@ end method show-help;
725671

726672
define method main (program, args)
727673
// Describe our arguments and create appropriate parser objects.
728-
let *argp* = make(<command-line-parser>);
729-
add-option(*argp*,
730-
make(<flag-option>,
731-
names: #("help", "h")));
674+
let *argp* = make(<command-line-parser>,
675+
help: "Melange");
732676
add-option(*argp*,
733677
make(<flag-option>,
678+
help: "Show copyright info.",
734679
names: #("version")));
735680
add-option(*argp*,
736681
make(<flag-option>,
682+
help: "Show the default C preprocessor definitions.",
737683
names: #("defines")));
738684
add-option(*argp*,
739685
make(<flag-option>,
686+
help: "Show the default platform undefinitions.",
740687
names: #("undefines")));
741688
add-option(*argp*,
742689
make(<flag-option>,
690+
help: "Show the default C preprocessor include directories.",
743691
names: #("includes")));
744692
add-option(*argp*,
745693
make(<flag-option>,
694+
help: "Print progress messages while parsing (includes --headers).",
746695
names: #("verbose", "v")));
747696
add-option(*argp*,
748697
make(<flag-option>,
698+
help: "Print each header file included while parsing.",
749699
names: #("headers")));
750700
add-option(*argp*,
751701
make(<choice-option>,
702+
help: "Generate output for use only with the named target. "
703+
"Target can be one of: c-ffi, mindy. [%default%]",
752704
names: #("target", "T"),
753705
choices: #("c-ffi", "mindy"),
754706
test: string-equal-ic?,
755707
default: "c-ffi"));
756708
add-option(*argp*,
757709
make(<parameter-option>,
710+
help: "Create a Dylan interchange file with a module definition "
711+
"that exports the interface names.",
758712
names: #("module-file", "m")));
759713
add-option(*argp*,
760714
make(<repeated-parameter-option>,
715+
help: "Extra directories to search for C headers.",
761716
names: #("includedir", "I")));
762717
add-option(*argp*,
763718
make(<keyed-option>,
719+
help: "Define a C preprocessor macro for use by C headers. "
720+
"If no value is given, defaults to 1.",
764721
names: #("define", "D")));
765722
add-option(*argp*,
766723
make(<repeated-parameter-option>,
724+
help: "Prevent definition of a default preprocessor macro. "
725+
"(Use --defines to see the defaults. Use --undefines to "
726+
"see the default undefines.)",
767727
names: #("undefine", "U")));
768728
add-option(*argp*,
769729
make(<repeated-parameter-option>,
730+
help: "The name of a framework bundle to search for C headers "
731+
"and child frameworks. Required when a child framework is directly "
732+
"referred to in an interface definition with no previous references "
733+
"to its parent; once a parent is seen, either via this option or in "
734+
"a clause of the interface definition, its children will be found "
735+
"automatically. (Note: Parent --framework options must be given "
736+
"before child framework options.)",
770737
names: #("framework")));
738+
add-option(*argp*,
739+
make(<positional-option>,
740+
help: "Input file with interface specification.",
741+
names: #("infile")));
742+
add-option(*argp*,
743+
make(<positional-option>,
744+
help: "Output file for generated Dylan code.",
745+
names: #("outfile"),
746+
required?: #f));
771747

772748
// Parse our command-line arguments.
773749
block ()
774750
parse-command-line(*argp*, args);
775-
exception (ex :: <usage-error>)
776-
show-usage-and-exit();
751+
exception (ex :: <abort-command-error>)
752+
exit-application(1);
777753
end;
778754

779755
// Handle our informational options.
@@ -789,10 +765,6 @@ define method main (program, args)
789765
show-default-includes(*standard-output*);
790766
exit-application(0);
791767
end if;
792-
if (get-option-value(*argp*, "help"))
793-
show-help(*standard-output*);
794-
exit-application(0);
795-
end if;
796768
if (get-option-value(*argp*, "version"))
797769
show-copyright(*standard-output*);
798770
exit-application(0);
@@ -806,7 +778,6 @@ define method main (program, args)
806778
let include-dirs = get-option-value(*argp*, "includedir");
807779
let defines = get-option-value(*argp*, "define");
808780
let undefines = get-option-value(*argp*, "undefine");
809-
let regular-args = positional-options(*argp*);
810781
let framework-dirs = get-option-value(*argp*, "framework");
811782

812783
// Handle --headers.
@@ -834,21 +805,13 @@ define method main (program, args)
834805
end for;
835806
find-frameworks(*framework-paths*);
836807

837-
// Handle regular arguments.
838-
let in-file = #f;
839-
let out-file = #f;
840-
select (regular-args.size)
841-
1 =>
842-
in-file := regular-args[0];
843-
2 =>
844-
in-file := regular-args[0];
845-
out-file := make(<file-stream>,
846-
locator: regular-args[1],
847-
direction: #"output");
848-
otherwise =>
849-
show-usage-and-exit();
850-
end select;
851-
808+
let in-file = get-option-value(*argp*, "infile");
809+
let out-file = get-option-value(*argp*, "outfile");
810+
if (out-file)
811+
out-file := make(<file-stream>,
812+
locator: out-file,
813+
direction: #"output");
814+
end;
852815
let module-stream = module-file & make(<file-stream>,
853816
locator: module-file,
854817
direction: #"output");

0 commit comments

Comments
 (0)