Changeset 360

Show
Ignore:
Timestamp:
01/20/08 18:34:39 (12 months ago)
Author:
tomoyo
Message:

コマンドラインパーサを使うようにしてみた。

Location:
TwitterIrcGateway/TwitterIrcGatewayCLI
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • TwitterIrcGateway/TwitterIrcGatewayCLI/Program.cs

    r357 r360  
    33using System.Text; 
    44using System.Net; 
    5 using Misuzilla.Applications.TwitterIrcGateway; 
    65using System.Reflection; 
    76using System.Threading; 
     7using System.ComponentModel; 
     8 
     9using Misuzilla.Applications.TwitterIrcGateway; 
     10using Misuzilla.Utilities; 
    811 
    912namespace TwitterIrcGatewayCLI 
     
    1114    class Program 
    1215    { 
     16        static CommandLineParser<CommandLineOptions> CommandLineParser = new CommandLineParser<CommandLineOptions>(); 
    1317        static void Main(string[] args) 
    1418        { 
    15             Boolean resolveTinyUrl = true; 
    16             Boolean enableDropProtection = true; 
    17             Boolean ignoreWatchError = false; 
    18             Boolean setTopicOnStatusChanged = false; 
    19             Boolean enableTrace = false; 
    20             Boolean cookieLoginMode = false; 
    21             Int32 interval = 60; 
    22             Int32 intervalDirectMessage = 180; 
    23             Int32 intervalReplies = 300; 
    24             Int32 port = 16668; 
    2519            IPAddress bindAddress = IPAddress.Loopback; 
    2620            Encoding encoding = Encoding.GetEncoding("ISO-2022-JP"); 
    27             String channelName = "#twitter"; 
    28             Boolean enableRepliesCheck = false; 
    2921 
    30             foreach (String arg in args) 
     22            CommandLineOptions options; 
     23            if (CommandLineParser.TryParse(args, out options)) 
    3124            { 
    32                 if (arg.StartsWith("--interval=")) 
    33                 { 
    34                     if (!Int32.TryParse(arg.Substring("--interval=".Length), out interval)) 
    35                     { 
    36                         interval = 60; 
    37                     } 
    38                 } 
    39                 else if (arg.StartsWith("--ignore-watch-error=")) 
    40                 { 
    41                     if (!Boolean.TryParse(arg.Substring("--ignore-watch-error=".Length), out ignoreWatchError)) 
    42                     { 
    43                         ignoreWatchError = false; 
    44                     } 
    45                 } 
    46                 else if (arg.StartsWith("--enable-trace=")) 
    47                 { 
    48                     if (!Boolean.TryParse(arg.Substring("--enable-trace=".Length), out enableTrace)) 
    49                     { 
    50                         enableTrace = false; 
    51                     } 
    52                 } 
    53                 else if (arg.StartsWith("--enable-replies-check=")) 
    54                 { 
    55                     if (!Boolean.TryParse(arg.Substring("--enable-replies-check=".Length), out enableRepliesCheck)) 
    56                     { 
    57                         enableRepliesCheck = false; 
    58                     } 
    59                 } 
    60                 else if (arg.StartsWith("--enable-drop-protection=")) 
    61                 { 
    62                     if (!Boolean.TryParse(arg.Substring("--enable-drop-protection=".Length), out enableDropProtection)) 
    63                     { 
    64                         enableDropProtection = true; 
    65                     } 
    66                 } 
    67                 else if (arg.StartsWith("--resolve-tinyurl=")) 
    68                 { 
    69                     if (!Boolean.TryParse(arg.Substring("--resolve-tinyurl=".Length), out resolveTinyUrl)) 
    70                     { 
    71                         resolveTinyUrl = true; 
    72                     } 
    73                 } 
    74                 else if (arg.StartsWith("--set-topic-onstatuschanged=")) 
    75                 { 
    76                     if (!Boolean.TryParse(arg.Substring("--set-topic-onstatuschanged=".Length), out setTopicOnStatusChanged)) 
    77                     { 
    78                         setTopicOnStatusChanged = false; 
    79                     } 
    80                 } 
    81                 else if (arg.StartsWith("--cookie-login-mode=")) 
    82                 { 
    83                     if (!Boolean.TryParse(arg.Substring("--cookie-login-mode=".Length), out cookieLoginMode)) 
    84                     { 
    85                         cookieLoginMode = false; 
    86                     } 
    87                 } 
    88                 else if (arg.StartsWith("--port=")) 
    89                 { 
    90                     if (!Int32.TryParse(arg.Substring("--port=".Length), out port)) 
    91                     { 
    92                         port = 16668; 
    93                     } 
    94                 } 
    95                 else if (arg.StartsWith("--bind-address=")) 
    96                 { 
    97                     if (!IPAddress.TryParse(arg.Substring("--bind-address=".Length), out bindAddress)) 
    98                     { 
    99                         bindAddress = IPAddress.Loopback; 
    100                     } 
    101                 } 
    102                 else if (arg.StartsWith("--encoding=")) 
    103                 { 
    104                     String encName = arg.Substring("--encoding=".Length); 
    105                     if (String.Compare(encName, "UTF-8", true) == 0) 
    106                         encoding = new UTF8Encoding(false); 
    107                     else 
    108                         encoding = Encoding.GetEncoding(encName); 
    109                 } 
    110                 else if (arg.StartsWith("--interval-directmessage=")) 
    111                 { 
    112                     if (!Int32.TryParse(arg.Substring("--interval-directmessage=".Length), out intervalDirectMessage)) 
    113                     { 
    114                         intervalDirectMessage = 180; 
    115                     } 
    116                 } 
    117                 else if (arg.StartsWith("--channel-name=")) 
    118                 { 
    119                     String channelNameT = arg.Substring("--channel-name=".Length).Replace(" ", ""); 
    120                     if (channelName.Length != 0) 
    121                     { 
    122                         channelName = "#"+channelNameT; 
    123                     } 
    124                 } 
    125                 else if (arg.StartsWith("--interval-replies=")) 
    126                 { 
    127                     if (!Int32.TryParse(arg.Substring("--interval-replies=".Length), out intervalReplies)) 
    128                     { 
    129                         intervalReplies = 300; 
    130                     } 
    131                 } 
    132                 else if (arg.StartsWith("--help")) 
     25                // Encoding 
     26                if (String.Compare(options.Encoding, "UTF-8", true) == 0) 
     27                    encoding = new UTF8Encoding(false); 
     28                else 
     29                    encoding = Encoding.GetEncoding(options.Encoding); 
     30 
     31                // Listening IP 
     32                if (!IPAddress.TryParse(options.BindAddress, out bindAddress)) 
    13333                { 
    13434                    ShowUsage(); 
     
    13636                } 
    13737            } 
     38            else 
     39            { 
     40                ShowUsage(); 
     41                return; 
     42            } 
    13843 
    13944            Server _server = new Server(); 
    140             _server.EnableTrace = enableTrace; 
    141             _server.IgnoreWatchError = ignoreWatchError; 
    142             _server.Interval = interval; 
    143             _server.ResolveTinyUrl = resolveTinyUrl; 
    144             _server.EnableDropProtection = enableDropProtection; 
     45            _server.EnableTrace = options.EnableTrace; 
     46            _server.IgnoreWatchError = options.IgnoreWatchError; 
     47            _server.Interval = options.Interval; 
     48            _server.ResolveTinyUrl = options.ResolveTinyurl; 
     49            _server.EnableDropProtection = options.EnableDropProtection; 
    14550            _server.Encoding = encoding; 
    146             _server.SetTopicOnStatusChanged = setTopicOnStatusChanged; 
    147             _server.IntervalDirectMessage = intervalDirectMessage; 
    148             _server.CookieLoginMode = cookieLoginMode; 
    149             _server.ChannelName = channelName; 
    150             _server.EnableRepliesCheck = enableRepliesCheck; 
    151             _server.IntervalReplies = intervalReplies; 
     51            _server.SetTopicOnStatusChanged = options.SetTopicOnstatuschanged; 
     52            _server.IntervalDirectMessage = options.IntervalDirectmessage; 
     53            _server.CookieLoginMode = options.CookieLoginMode; 
     54            _server.ChannelName = "#"+options.ChannelName; 
     55            _server.EnableRepliesCheck = options.EnableRepliesCheck; 
     56            _server.IntervalReplies = options.IntervalReplies; 
    15257            _server.SessionStartedRecieved += new EventHandler<SessionStartedEventArgs>(_server_SessionStartedRecieved); 
    15358 
    15459            Console.WriteLine("Start TwitterIrcGateway Server v{0}", typeof(Server).Assembly.GetName().Version); 
    155             Console.WriteLine("[Configuration] BindAddress: {0}, Port: {1}", bindAddress, port); 
     60            Console.WriteLine("[Configuration] BindAddress: {0}, Port: {1}", bindAddress, options.Port); 
    15661            Console.WriteLine("[Configuration] EnableTrace: {0}", _server.EnableTrace); 
    15762            Console.WriteLine("[Configuration] IgnoreWatchError: {0}", _server.IgnoreWatchError); 
     
    16772            Console.WriteLine("[Configuration] IntervalReplies: {0}", _server.IntervalReplies); 
    16873 
    169             _server.Start(bindAddress, port); 
     74            _server.Start(bindAddress, options.Port); 
    17075 
    17176            while (true) 
     
    17681        { 
    17782            Console.WriteLine("TwitterIrcGateway Server v{0}", typeof(Server).Assembly.GetName().Version); 
    178             Console.WriteLine( 
    179 @" 
    180 Usage: TwitterIrcGateway [--port=<port>] [--bind-address=<bindaddr>] [--interval=<sec>] [--resolve-tinyurl=<true|false>] [--encoding=<encoding-name>] [--ignore-watch-error=<true|false>] [--enable-drop-protection=<true|false>] [--set-topic-onstatuschanged=<true|false>] [--enable-trace=<true|false>] [--interval-directmessage=<sec>] [--cookie-login-mode=<true|false>] [--channel-name=<ChannelName>] [--interval-replies=<sec>] 
    181  
    182 --port=<port>                            : IRC server listen port (default: 16668) 
    183 --bind-address=<bindaddr>                : IRC server bind IP address (default: 127.0.0.1) 
    184 --interval=<sec>                         : interval of checking Timeline (default: 90) 
    185 --resolve-tinyurl=<true|false>           : enable TinyURL resolver (default: true) 
    186 --encoding=<encoding>                    : IRC message text character encoding (default: ISO-2022-JP) 
    187 --ignore-watch-error=<true|false>        : ignore API error messages (default: false) 
    188 --enable-drop-protection=<true|false>    : enable drop protection (default: true) 
    189 --set-topic-onstatuschanged=<true|false> : set status as topic on status changed (default: false) 
    190 --enable-trace=<true|false>              : enable trace (default: false) 
    191 --interval-directmessage=<sec>           : interval of checking directmessage (default: 180) 
    192 --cookie-login-mode=<true|false>         : enable cookie-login mode (default: false) 
    193 --channel-name=<ChannelName>             : channel name of Twitter timeline (default: Twitter) 
    194 --enable-replies-check=<true|false>      : enable replies check (default: false) 
    195 --interval-replies=<sec>                 : interval of checking Replies (default: 300) 
    196 "); 
     83            Console.WriteLine(@"Usage:"); 
     84            CommandLineParser.ShowHelp(); 
    19785        } 
    19886 
     
    20290        } 
    20391    } 
     92 
     93    class CommandLineOptions 
     94    { 
     95        [DefaultValue(16668)] 
     96        [Description("IRC server listen port")] 
     97        public Int32 Port { get; set; } 
     98 
     99        [DefaultValue("127.0.0.1")] 
     100        [Description("IRC server bind IP address")] 
     101        public String BindAddress { get; set; } 
     102 
     103        [DefaultValue(90)] 
     104        [Description("interval of checking Timeline")] 
     105        public Int32 Interval { get; set; } 
     106 
     107        [DefaultValue(true)] 
     108        [Description("enable TinyURL resolver")] 
     109        public Boolean ResolveTinyurl { get; set; } 
     110 
     111        [DefaultValue("ISO-2022-JP")] 
     112        [Description("IRC message text character encoding")] 
     113        public String Encoding { get; set; } 
     114 
     115        [DefaultValue(false)] 
     116        [Description("ignore API error messages")] 
     117        public Boolean IgnoreWatchError { get; set; } 
     118 
     119        [DefaultValue(true)] 
     120        [Description("enable drop protection")] 
     121        public Boolean EnableDropProtection { get; set; } 
     122 
     123        [DefaultValue(false)] 
     124        [Description("set status as topic on status changed")] 
     125        public Boolean SetTopicOnstatuschanged { get; set; } 
     126 
     127        [DefaultValue(false)] 
     128        [Description("enable trace")] 
     129        public Boolean EnableTrace { get; set; } 
     130 
     131        [DefaultValue(180)] 
     132        [Description("interval of checking directmessage")] 
     133        public Int32 IntervalDirectmessage { get; set; } 
     134 
     135        [DefaultValue(false)] 
     136        [Description("enable cookie-login mode")] 
     137        public Boolean CookieLoginMode { get; set; } 
     138 
     139        [DefaultValue("Twitter")] 
     140        [Description("channel name of Twitter timeline")] 
     141        public String ChannelName { get; set; } 
     142 
     143        [DefaultValue(false)] 
     144        [Description("enable replies check")] 
     145        public Boolean EnableRepliesCheck { get; set; } 
     146 
     147        [DefaultValue(300)] 
     148        [Description("interval of checking Replies")] 
     149        public Int32 IntervalReplies { get; set; } 
     150    } 
    204151} 
  • TwitterIrcGateway/TwitterIrcGatewayCLI/TwitterIrcGatewayCLI.csproj

    r358 r360  
    33    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> 
    44    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> 
    5     <ProductVersion>8.0.50727</ProductVersion> 
     5    <ProductVersion>9.0.21022</ProductVersion> 
    66    <SchemaVersion>2.0</SchemaVersion> 
    77    <ProjectGuid>{4FCEDBD3-FA87-4ADE-82E9-B3C5BA431F54}</ProjectGuid> 
     
    1515    <UpgradeBackupLocation> 
    1616    </UpgradeBackupLocation> 
     17    <PublishUrl>publish\</PublishUrl> 
     18    <Install>true</Install> 
     19    <InstallFrom>Disk</InstallFrom> 
     20    <UpdateEnabled>false</UpdateEnabled> 
     21    <UpdateMode>Foreground</UpdateMode> 
     22    <UpdateInterval>7</UpdateInterval> 
     23    <UpdateIntervalUnits>Days</UpdateIntervalUnits> 
     24    <UpdatePeriodically>false</UpdatePeriodically> 
     25    <UpdateRequired>false</UpdateRequired> 
     26    <MapFileExtensions>true</MapFileExtensions> 
     27    <ApplicationRevision>0</ApplicationRevision> 
     28    <ApplicationVersion>1.0.0.%2a</ApplicationVersion> 
     29    <IsWebBootstrapper>false</IsWebBootstrapper> 
     30    <UseApplicationTrust>false</UseApplicationTrust> 
     31    <BootstrapperEnabled>true</BootstrapperEnabled> 
    1732  </PropertyGroup> 
    1833  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> 
     
    3853  </ItemGroup> 
    3954  <ItemGroup> 
     55    <Compile Include="CommandLineParser.cs" /> 
    4056    <Compile Include="Program.cs" /> 
    4157    <Compile Include="Properties\AssemblyInfo.cs" /> 
     
    4662      <Name>TwitterIrcGatewayCore</Name> 
    4763    </ProjectReference> 
     64  </ItemGroup> 
     65  <ItemGroup> 
     66    <BootstrapperPackage Include="Microsoft.Net.Framework.2.0"> 
     67      <Visible>False</Visible> 
     68      <ProductName>.NET Framework 2.0 %28x86%29</ProductName> 
     69      <Install>true</Install> 
     70    </BootstrapperPackage> 
     71    <BootstrapperPackage Include="Microsoft.Net.Framework.3.0"> 
     72      <Visible>False</Visible> 
     73      <ProductName>.NET Framework 3.0 %28x86%29</ProductName> 
     74      <Install>false</Install> 
     75    </BootstrapperPackage> 
     76    <BootstrapperPackage Include="Microsoft.Net.Framework.3.5"> 
     77      <Visible>False</Visible> 
     78      <ProductName>.NET Framework 3.5</ProductName> 
     79      <Install>false</Install> 
     80    </BootstrapperPackage> 
    4881  </ItemGroup> 
    4982  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />