| 1 |
// SvnTest, a client program used to test SubversionSharp library |
|---|
| 2 |
#region Copyright (C) 2004 SOFTEC sa. |
|---|
| 3 |
// |
|---|
| 4 |
// SvnTest, a client program used to test SubversionSharp library |
|---|
| 5 |
// Copyright 2004 by SOFTEC sa |
|---|
| 6 |
// |
|---|
| 7 |
// This program is free software; you can redistribute it and/or |
|---|
| 8 |
// modify it under the terms of the GNU General Public License as |
|---|
| 9 |
// published by the Free Software Foundation; either version 2 of |
|---|
| 10 |
// the License, or (at your option) any later version. |
|---|
| 11 |
// |
|---|
| 12 |
// This program is distributed in the hope that it will be useful, |
|---|
| 13 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 15 |
// See the GNU General Public License for more details. |
|---|
| 16 |
// |
|---|
| 17 |
// You should have received a copy of the GNU General Public |
|---|
| 18 |
// License along with this program; if not, write to the Free Software |
|---|
| 19 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 |
// |
|---|
| 21 |
// Sources, support options and lastest version of the complete library |
|---|
| 22 |
// is available from: |
|---|
| 23 |
// http://www.softec.st/SubversionSharp |
|---|
| 24 |
// |
|---|
| 25 |
// |
|---|
| 26 |
// Initial authors : |
|---|
| 27 |
// Denis Gervalle |
|---|
| 28 |
// Olivier Desaive |
|---|
| 29 |
#endregion |
|---|
| 30 |
// |
|---|
| 31 |
using System; |
|---|
| 32 |
using System.Collections; |
|---|
| 33 |
using System.Reflection; |
|---|
| 34 |
using Mono.GetOptions; |
|---|
| 35 |
|
|---|
| 36 |
namespace Softec.SubversionSharp.Test { |
|---|
| 37 |
|
|---|
| 38 |
class Application : Options |
|---|
| 39 |
{ |
|---|
| 40 |
public class SubCommand { |
|---|
| 41 |
public string LongName; |
|---|
| 42 |
public string ShortName; |
|---|
| 43 |
public string Description; |
|---|
| 44 |
public Type Impl; |
|---|
| 45 |
public SubCommand(string longName, string shortName, string desc, Type impl) |
|---|
| 46 |
{ |
|---|
| 47 |
LongName = longName; |
|---|
| 48 |
ShortName = shortName; |
|---|
| 49 |
Description = desc; |
|---|
| 50 |
Impl = impl; |
|---|
| 51 |
} |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
private SortedList SubCommands = new SortedList(); |
|---|
| 55 |
private SortedList ShortCommands = new SortedList(); |
|---|
| 56 |
private bool mPendingHelp = false; |
|---|
| 57 |
|
|---|
| 58 |
public Application() |
|---|
| 59 |
{ |
|---|
| 60 |
Assembly a = Assembly.GetCallingAssembly(); |
|---|
| 61 |
foreach(Type t in a.GetTypes()) |
|---|
| 62 |
{ |
|---|
| 63 |
object[] o; |
|---|
| 64 |
if((o = t.GetCustomAttributes(typeof(SubCommandAttribute),true)) != null |
|---|
| 65 |
&& o.Length != 0) |
|---|
| 66 |
{ |
|---|
| 67 |
SubCommandAttribute sc = (SubCommandAttribute)o[0]; |
|---|
| 68 |
SubCommands.Add(sc.LongName,new SubCommand(sc.LongName,sc.ShortName,sc.Description,t)); |
|---|
| 69 |
if (sc.ShortName != string.Empty) |
|---|
| 70 |
ShortCommands.Add(sc.ShortName,sc.LongName); |
|---|
| 71 |
} |
|---|
| 72 |
} |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
private WhatToDoNext UsageAppend(WhatToDoNext ret) |
|---|
| 76 |
{ |
|---|
| 77 |
Console.Write("Subcommands: "); |
|---|
| 78 |
bool addSep = false; |
|---|
| 79 |
foreach(SubCommand sc in SubCommands.Values) |
|---|
| 80 |
{ |
|---|
| 81 |
if( addSep ) |
|---|
| 82 |
Console.Write(", "); |
|---|
| 83 |
else |
|---|
| 84 |
addSep = true; |
|---|
| 85 |
Console.Write(sc.LongName); |
|---|
| 86 |
if( sc.ShortName != string.Empty ) |
|---|
| 87 |
Console.Write("[{0}]",sc.ShortName); |
|---|
| 88 |
} |
|---|
| 89 |
Console.WriteLine("\nFor help on subcommands, use the -?/--help subcommand option."); |
|---|
| 90 |
return(ret); |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
public override WhatToDoNext DoUsage() |
|---|
| 94 |
{ |
|---|
| 95 |
return WhatToDoNext.GoAhead; |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
public override WhatToDoNext DoHelp() |
|---|
| 99 |
{ |
|---|
| 100 |
mPendingHelp = true; |
|---|
| 101 |
return WhatToDoNext.GoAhead; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
public int Run(string[] args) |
|---|
| 105 |
{ |
|---|
| 106 |
ProcessArgs(args); |
|---|
| 107 |
|
|---|
| 108 |
if( RemainingArguments.Length == 0 ) |
|---|
| 109 |
{ |
|---|
| 110 |
if( mPendingHelp ) |
|---|
| 111 |
UsageAppend(base.DoHelp()); |
|---|
| 112 |
else |
|---|
| 113 |
UsageAppend(base.DoUsage()); |
|---|
| 114 |
return(0); |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
SubCommand sc; |
|---|
| 118 |
if ((sc = (SubCommand) SubCommands[RemainingArguments[0].ToLower()]) == null) |
|---|
| 119 |
{ |
|---|
| 120 |
string shortCmd = (string) ShortCommands[RemainingArguments[0].ToLower()]; |
|---|
| 121 |
if( shortCmd == null |
|---|
| 122 |
|| (sc = (SubCommand) SubCommands[shortCmd]) == null ) |
|---|
| 123 |
{ |
|---|
| 124 |
if( mPendingHelp ) |
|---|
| 125 |
UsageAppend(base.DoHelp()); |
|---|
| 126 |
else |
|---|
| 127 |
UsageAppend(base.DoUsage()); |
|---|
| 128 |
return(1); |
|---|
| 129 |
} |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
ConstructorInfo ctor = sc.Impl.GetConstructor(new Type[0]); |
|---|
| 133 |
MethodInfo run = sc.Impl.GetMethod("Run",new Type[] { typeof(SubCommand), typeof(string[]) }); |
|---|
| 134 |
|
|---|
| 135 |
if(ctor == null || run == null || run.ReturnType != typeof(int)) |
|---|
| 136 |
throw new InvalidOperationException("Invalid subcommand class"); |
|---|
| 137 |
|
|---|
| 138 |
return((int)run.Invoke(ctor.Invoke(new object[0]),new object[] { sc, args })); |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
public static int Main(string[] args) |
|---|
| 142 |
{ |
|---|
| 143 |
Application progInst = new Application(); |
|---|
| 144 |
return( progInst.Run(args) ); |
|---|
| 145 |
} |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
[AttributeUsage(AttributeTargets.Class)] |
|---|
| 149 |
public class SubCommandAttribute : Attribute |
|---|
| 150 |
{ |
|---|
| 151 |
public string ShortName; |
|---|
| 152 |
public string LongName; |
|---|
| 153 |
public string Description; |
|---|
| 154 |
|
|---|
| 155 |
public SubCommandAttribute(string longName) |
|---|
| 156 |
{ |
|---|
| 157 |
ShortName = string.Empty; |
|---|
| 158 |
LongName = longName.ToLower(); |
|---|
| 159 |
Description = string.Empty; |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
public SubCommandAttribute(string longName, string desc) |
|---|
| 163 |
{ |
|---|
| 164 |
ShortName = string.Empty; |
|---|
| 165 |
LongName = longName.ToLower(); |
|---|
| 166 |
Description = desc; |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
public SubCommandAttribute(string longName, string shortName, string desc) |
|---|
| 170 |
{ |
|---|
| 171 |
ShortName = shortName.ToLower(); |
|---|
| 172 |
LongName = longName.ToLower(); |
|---|
| 173 |
Description = desc; |
|---|
| 174 |
} |
|---|
| 175 |
} |
|---|
| 176 |
} |
|---|