Re: [cp-patches] FYI: jarsigner now uses getopt parser

2006-05-14 Thread Tom Tromey
 Raif == Raif S Naffah [EMAIL PROTECTED] writes:

Raif the attached patch --already committed-- replaces command line parsing 
Raif in the jarsigner tool with the newly added getopt classes.

I have a few comments on this.

Raifprivate Main()
[...]
Raif  try
Raif{
Raif  tool.processArgs(args);
Raif  tool.start();
Raif +result = 0;
Raif}
Raif +catch (OptionException x)
Raif +{
Raif +  System.err.println(x.getMessage()); //$NON-NLS-1$
Raif +  if (tool.cmdLineParser != null)
Raif +tool.cmdLineParser.printHelp();
Raif +}

Generally speaking GNU tools do not print --help output in response
to an error.  Instead they typically just print a short error message
and a note to try --help.

Parser will do this for you automatically now; I recommend
implementing the new validate() method to do post-parsing option
checks.

Raifprivate void processArgs(String[] args) throws Exception

Raif  if (args == null || args.length == 0)
Raif -  HelpPrinter.printHelpAndExit(HELP_PATH);
Raif +  throw new OptionException(Messages.getString(Main.133)); 
//$NON-NLS-1$

E.g., do this kind of thing in validate().


Also, I noticed that keytool isn't really using the new parser -- just
the help printer.  Is there a way we can extend the parser to handle
the cases needed by keytool?  Honestly I'd prefer not to export the
printHelp functionality from the parser, and this seems to be the only
external user.  Also, since getopt really exists only for classpath
tools, it would be better to just extend it as needed to do what we
really want.  (BTW if we're going to export the printHelp method, it
ought to have javadoc -- I'm trying to document all the exposed API of
getopt.)

Tom



[cp-patches] FYI: jarsigner now uses getopt parser

2006-05-12 Thread Raif S. Naffah
hello all,

the attached patch --already committed-- replaces command line parsing 
in the jarsigner tool with the newly added getopt classes.

2006-05-13  Raif S. Naffah  [EMAIL PROTECTED]

* resource/gnu/classpath/tools/jarsigner/MessageBundle.properties:
Added help text.
* tools/Makefile.am (JARSIGNER_HELPS): Removed.
* tools/gnu/classpath/tools/jarsigner/jarsigner.txt: Removed.
* tools/gnu/classpath/tools/jarsigner/Main.java:
Increased visibility of fields used by parser anonymous classes.
(HELP_PATH): Removed.
(cmdLineParser): New field.
(main): Handle JVM exit status.
Handle command line parsing exceptions.
(processArgs): Use getopt command line parser.
(getParser): New method.
(setupCommonParams): Removed checks now handled by processArgs().
(setupSigningParams): Likewise.
* tools/gnu/classpath/tools/jarsigner/JarSigner.java (start):
Reuse an existing message-bundle constant.


cheers;
rsn
Index: JarSigner.java
===
RCS file: /cvsroot/classpath/classpath/tools/gnu/classpath/tools/jarsigner/JarSigner.java,v
retrieving revision 1.5
diff -u -r1.5 JarSigner.java
--- JarSigner.java	2 May 2006 01:00:22 -	1.5
+++ JarSigner.java	12 May 2006 23:58:21 -
@@ -141,7 +141,7 @@
   main.isInternalSF());
 log.finer(Created .DSA file); //$NON-NLS-1$
 if (main.isVerbose())
-  System.out.println(Messages.getString(JarSigner.11) + dsaFileName); //$NON-NLS-1$
+  System.out.println(Messages.getString(JarSigner.8) + dsaFileName); //$NON-NLS-1$

 // cleanup
 outSignedJarFile.close();
Index: Main.java
===
RCS file: /cvsroot/classpath/classpath/tools/gnu/classpath/tools/jarsigner/Main.java,v
retrieving revision 1.9
diff -u -r1.9 Main.java
--- Main.java	3 May 2006 05:40:24 -	1.9
+++ Main.java	13 May 2006 00:24:57 -
@@ -39,9 +39,13 @@
 package gnu.classpath.tools.jarsigner;

 import gnu.classpath.SystemProperties;
-import gnu.classpath.tools.HelpPrinter;
 import gnu.classpath.tools.common.CallbackUtil;
 import gnu.classpath.tools.common.ProviderUtil;
+import gnu.classpath.tools.getopt.ClasspathToolParser;
+import gnu.classpath.tools.getopt.Option;
+import gnu.classpath.tools.getopt.OptionException;
+import gnu.classpath.tools.getopt.OptionGroup;
+import gnu.classpath.tools.getopt.Parser;
 import gnu.java.security.OID;
 import gnu.java.security.Registry;
 import gnu.javax.security.auth.callback.ConsoleCallbackHandler;
@@ -82,7 +86,6 @@
 public class Main
 {
   private static final Logger log = Logger.getLogger(Main.class.getName());
-  private static final String HELP_PATH = jarsigner/jarsigner.txt; //$NON-NLS-1$
   private static final Locale EN_US_LOCALE = new Locale(en, US); //$NON-NLS-1$ //$NON-NLS-2$
   static final String DIGEST = SHA1-Digest; //$NON-NLS-1$
   static final String DIGEST_MANIFEST = SHA1-Digest-Manifest; //$NON-NLS-1$
@@ -91,18 +94,18 @@
   static final OID DSA_SIGNATURE_OID = new OID(Registry.DSA_OID_STRING);
   static final OID RSA_SIGNATURE_OID = new OID(Registry.RSA_OID_STRING);

-  private boolean verify;
-  private String ksURL;
-  private String ksType;
-  private String password;
-  private String ksPassword;
-  private String sigFileName;
-  private String signedJarFileName;
-  private boolean verbose;
-  private boolean certs;
-  private boolean internalSF;
-  private boolean sectionsOnly;
-  private String providerClassName;
+  protected boolean verify;
+  protected String ksURL;
+  protected String ksType;
+  protected String password;
+  protected String ksPassword;
+  protected String sigFileName;
+  protected String signedJarFileName;
+  protected boolean verbose;
+  protected boolean certs;
+  protected boolean internalSF;
+  protected boolean sectionsOnly;
+  protected String providerClassName;
   private String jarFileName;
   private String alias;

@@ -115,6 +118,8 @@
   private Certificate[] signerCertificateChain;
   /** The callback handler to use when needing to interact with user. */
   private CallbackHandler handler;
+  /** The command line parser. */
+  private Parser cmdLineParser;

   private Main()
   {
@@ -126,11 +131,19 @@
 log.entering(Main.class.getName(), main, args); //$NON-NLS-1$

 Main tool = new Main();
+int result = 1;
 try
   {
 tool.processArgs(args);
 tool.start();
+result = 0;
   }
+catch (OptionException x)
+{
+  System.err.println(x.getMessage()); //$NON-NLS-1$
+  if (tool.cmdLineParser != null)
+tool.cmdLineParser.printHelp();
+}
 catch (SecurityException x)
   {
 log.throwing(Main.class.getName(), main, x); //$NON-NLS-1$
@@ -141,11 +154,13 @@
 log.throwing(Main.class.getName(), main, x); //$NON-NLS-1$