Mercurial > jhg
changeset 299:45dc79e545f5
Recognize flag options (options with no arguments) in command line sample apps
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Sat, 17 Sep 2011 13:26:52 +0200 |
parents | aac0c3fab6ce |
children | 650b45d290b1 |
files | cmdline/org/tmatesoft/hg/console/Bundle.java cmdline/org/tmatesoft/hg/console/Cat.java cmdline/org/tmatesoft/hg/console/Clone.java cmdline/org/tmatesoft/hg/console/Incoming.java cmdline/org/tmatesoft/hg/console/Log.java cmdline/org/tmatesoft/hg/console/Main.java cmdline/org/tmatesoft/hg/console/Manifest.java cmdline/org/tmatesoft/hg/console/Options.java cmdline/org/tmatesoft/hg/console/Outgoing.java cmdline/org/tmatesoft/hg/console/Status.java cmdline/org/tmatesoft/hg/console/Tags.java |
diffstat | 11 files changed, 44 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/cmdline/org/tmatesoft/hg/console/Bundle.java Sat Sep 17 12:24:50 2011 +0200 +++ b/cmdline/org/tmatesoft/hg/console/Bundle.java Sat Sep 17 13:26:52 2011 +0200 @@ -17,6 +17,7 @@ package org.tmatesoft.hg.console; import java.io.File; +import java.util.Collections; import org.tmatesoft.hg.core.Nodeid; import org.tmatesoft.hg.repo.HgBundle; @@ -34,7 +35,7 @@ */ public class Bundle { public static void main(String[] args) throws Exception { - Options cmdLineOpts = Options.parse(args); + Options cmdLineOpts = Options.parse(args, Collections.<String>emptySet()); final HgRepository hgRepo = cmdLineOpts.findRepository(); if (hgRepo.isInvalid()) { System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());
--- a/cmdline/org/tmatesoft/hg/console/Cat.java Sat Sep 17 12:24:50 2011 +0200 +++ b/cmdline/org/tmatesoft/hg/console/Cat.java Sat Sep 17 13:26:52 2011 +0200 @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; +import java.util.Collections; import org.tmatesoft.hg.repo.HgDataFile; import org.tmatesoft.hg.repo.HgRepository; @@ -34,7 +35,7 @@ public class Cat { public static void main(String[] args) throws Exception { - Options cmdLineOpts = Options.parse(args); + Options cmdLineOpts = Options.parse(args, Collections.<String>emptySet()); HgRepository hgRepo = cmdLineOpts.findRepository(); if (hgRepo.isInvalid()) { System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());
--- a/cmdline/org/tmatesoft/hg/console/Clone.java Sat Sep 17 12:24:50 2011 +0200 +++ b/cmdline/org/tmatesoft/hg/console/Clone.java Sat Sep 17 13:26:52 2011 +0200 @@ -17,6 +17,7 @@ package org.tmatesoft.hg.console; import java.io.File; +import java.util.Collections; import java.util.List; import org.tmatesoft.hg.core.HgCloneCommand; @@ -33,7 +34,7 @@ // ran with args: svnkit c:\temp\hg\test-clone public static void main(String[] args) throws Exception { - Options cmdLineOpts = Options.parse(args); + Options cmdLineOpts = Options.parse(args, Collections.<String>emptySet()); List<String> noOptsArgs = cmdLineOpts.getList(""); if (noOptsArgs.isEmpty()) { System.err.println("Need at least one argument pointing to remote server to pull changes from");
--- a/cmdline/org/tmatesoft/hg/console/Incoming.java Sat Sep 17 12:24:50 2011 +0200 +++ b/cmdline/org/tmatesoft/hg/console/Incoming.java Sat Sep 17 13:26:52 2011 +0200 @@ -46,7 +46,7 @@ new SequenceConstructor().test(); return; } - Options cmdLineOpts = Options.parse(args); + Options cmdLineOpts = Options.parse(args, Collections.<String>emptySet()); HgRepoFacade hgRepo = new HgRepoFacade(); if (!hgRepo.init(cmdLineOpts.findRepository())) { System.err.printf("Can't find repository in: %s\n", hgRepo.getRepository().getLocation());
--- a/cmdline/org/tmatesoft/hg/console/Log.java Sat Sep 17 12:24:50 2011 +0200 +++ b/cmdline/org/tmatesoft/hg/console/Log.java Sat Sep 17 13:26:52 2011 +0200 @@ -16,6 +16,8 @@ */ package org.tmatesoft.hg.console; +import static org.tmatesoft.hg.console.Options.asSet; + import java.util.List; import org.tmatesoft.hg.core.HgFileRevision; @@ -35,7 +37,7 @@ // -agentlib:hprof=heap=sites,depth=10,etc might be handy to debug speed/memory issues public static void main(String[] args) throws Exception { - Options cmdLineOpts = Options.parse(args); + Options cmdLineOpts = Options.parse(args, asSet("--debug", "-v", "--verbose", "--hg4j-order-direct")); HgRepository hgRepo = cmdLineOpts.findRepository(); if (hgRepo.isInvalid()) { System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation()); @@ -49,7 +51,7 @@ final Dump dump = new Dump(hgRepo); dump.complete(cmdLineOpts.getBoolean("--debug")); dump.verbose(cmdLineOpts.getBoolean("-v", "--verbose")); - final boolean reverseOrder = true; + final boolean reverseOrder = !cmdLineOpts.getBoolean("--hg4j-order-direct"); dump.reversed(reverseOrder); HgLogCommand cmd = new HgLogCommand(hgRepo); for (String u : cmdLineOpts.getList("-u", "--user")) {
--- a/cmdline/org/tmatesoft/hg/console/Main.java Sat Sep 17 12:24:50 2011 +0200 +++ b/cmdline/org/tmatesoft/hg/console/Main.java Sat Sep 17 13:26:52 2011 +0200 @@ -74,7 +74,7 @@ private HgRepository hgRepo; public Main(String[] args) throws Exception { - cmdLineOpts = Options.parse(args); + cmdLineOpts = Options.parse(args, Collections.<String>emptySet()); hgRepo = cmdLineOpts.findRepository(); if (hgRepo.isInvalid()) { System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());
--- a/cmdline/org/tmatesoft/hg/console/Manifest.java Sat Sep 17 12:24:50 2011 +0200 +++ b/cmdline/org/tmatesoft/hg/console/Manifest.java Sat Sep 17 13:26:52 2011 +0200 @@ -16,6 +16,7 @@ */ package org.tmatesoft.hg.console; +import static org.tmatesoft.hg.console.Options.asSet; import static org.tmatesoft.hg.repo.HgRepository.TIP; import org.tmatesoft.hg.core.HgFileRevision; @@ -33,7 +34,7 @@ public class Manifest { public static void main(String[] args) throws Exception { - Options cmdLineOpts = Options.parse(args); + Options cmdLineOpts = Options.parse(args, asSet("--debug", "-v", "--verbose")); HgRepository hgRepo = cmdLineOpts.findRepository(); if (hgRepo.isInvalid()) { System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());
--- a/cmdline/org/tmatesoft/hg/console/Options.java Sat Sep 17 12:24:50 2011 +0200 +++ b/cmdline/org/tmatesoft/hg/console/Options.java Sat Sep 17 13:26:52 2011 +0200 @@ -16,10 +16,13 @@ */ package org.tmatesoft.hg.console; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.TreeSet; import org.tmatesoft.hg.repo.HgLookup; import org.tmatesoft.hg.repo.HgRepository; @@ -86,7 +89,7 @@ } - public static Options parse(String[] commandLineArgs) { + public static Options parse(String[] commandLineArgs, Set<String> flagOptions) { Options rv = new Options(); List<String> values = new LinkedList<String>(); rv.opt2values.put("", values); // values with no options @@ -96,9 +99,14 @@ if (arg.length() == 1) { throw new IllegalArgumentException("Bad option: -"); } - values = rv.opt2values.get(arg); - if (values == null) { - rv.opt2values.put(arg, values = new LinkedList<String>()); + if (flagOptions.contains(arg)) { + rv.opt2values.put(arg, Collections.<String>emptyList()); + values = rv.opt2values.get(""); + } else { + values = rv.opt2values.get(arg); + if (values == null) { + rv.opt2values.put(arg, values = new LinkedList<String>()); + } } // next value, if any, gets into the values list for arg option. } else { @@ -108,4 +116,12 @@ } return rv; } + + public static Set<String> asSet(String... ss) { + TreeSet<String> rv = new TreeSet<String>(); + for (String s : ss) { + rv.add(s); + } + return rv; + } } \ No newline at end of file
--- a/cmdline/org/tmatesoft/hg/console/Outgoing.java Sat Sep 17 12:24:50 2011 +0200 +++ b/cmdline/org/tmatesoft/hg/console/Outgoing.java Sat Sep 17 13:26:52 2011 +0200 @@ -17,6 +17,7 @@ package org.tmatesoft.hg.console; import java.util.Collection; +import java.util.Collections; import java.util.List; import org.tmatesoft.hg.core.HgOutgoingCommand; @@ -35,7 +36,7 @@ public class Outgoing { public static void main(String[] args) throws Exception { - Options cmdLineOpts = Options.parse(args); + Options cmdLineOpts = Options.parse(args, Collections.<String>emptySet()); HgRepoFacade hgRepo = new HgRepoFacade(); if (!hgRepo.init(cmdLineOpts.findRepository())) { System.err.printf("Can't find repository in: %s\n", hgRepo.getRepository().getLocation());
--- a/cmdline/org/tmatesoft/hg/console/Status.java Sat Sep 17 12:24:50 2011 +0200 +++ b/cmdline/org/tmatesoft/hg/console/Status.java Sat Sep 17 13:26:52 2011 +0200 @@ -16,6 +16,7 @@ */ package org.tmatesoft.hg.console; +import static org.tmatesoft.hg.console.Options.asSet; import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; import java.util.ArrayList; @@ -25,6 +26,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; import org.tmatesoft.hg.core.HgRepoFacade; import org.tmatesoft.hg.core.HgStatus; @@ -40,7 +42,10 @@ public class Status { public static void main(String[] args) throws Exception { - Options cmdLineOpts = Options.parse(args); + final Set<String> flagOpts = asSet("-A", "-all", "-m", "--modified", "-a", "--added", "-r", "--removed", + "--d", "--deleted", "-u", "--unknown", "-c", "--clean", "-i", "--ignored", + "-n", "--no-status", "-C", "--copies"); + Options cmdLineOpts = Options.parse(args, flagOpts); HgRepoFacade hgRepo = new HgRepoFacade(); if (!hgRepo.init(cmdLineOpts.findRepository())) { System.err.printf("Can't find repository in: %s\n", hgRepo.getRepository().getLocation());
--- a/cmdline/org/tmatesoft/hg/console/Tags.java Sat Sep 17 12:24:50 2011 +0200 +++ b/cmdline/org/tmatesoft/hg/console/Tags.java Sat Sep 17 13:26:52 2011 +0200 @@ -16,6 +16,7 @@ */ package org.tmatesoft.hg.console; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Map; @@ -34,7 +35,7 @@ public class Tags { public static void main(String[] args) throws Exception { - Options cmdLineOpts = Options.parse(args); + Options cmdLineOpts = Options.parse(args, Collections.<String>emptySet()); HgRepository hgRepo = cmdLineOpts.findRepository(); if (hgRepo.isInvalid()) { System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());