- 浏览: 78028 次
- 性别:
文章分类
最新评论
-
peikona:
从文件系统中加载python模块。另外setup中使用zipf ...
py2exe越来越爽了 -
davidsun2008:
[size=large][size=medium]大家好,py ...
py2exe越来越爽了 -
jinheking:
我会尝试着把模式详尽剖析。不过能力有限,望大家多提宝贵意见。
DLang策略模式 -
DavidL:
不错的理念,赞一个。
能详尽剖析就更好了
DLang策略模式 -
shawind:
可以像很多D项目一样,自己写一个all.d,在这里面impor ...
DLang策略模式
我临摹的一个 hello world。
大家看了以后不要说我无聊,临摹的是:
http://packages.debian.org/unstable/devel/hello
getopt.d
hello.d
dmd hello.d getopt.d -D
大家看了以后不要说我无聊,临摹的是:
http://packages.debian.org/unstable/devel/hello
getopt.d
/** * Copyright (c) 2004 * Juanjo Alvarez Martinez <juanjux@yahoo.es> * Copyright (c) 2007 only Version:0.5 * jinheking <jinheking@gmail.com> * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. Author makes no representations about * the suitability of this software for any purpose. It is provided * "as is" without express or implied warranty. */ /* Based (copied of) on the Python language getopt implementation. It implements * a POSIX-alike getopt, that is, it stops parsing the options as soon as it * find a non-option argument, and removes the options it parses from args[][] so the * application is left with all the nonoption arguments in args. If you don't want to use * short options or long options just change the argument for "null". * * Version: 0.5 * * Changes: * * 0.1 Initial release * 0.2 Lot's of bugfixing, "posixification" and improvements thanks to Jonathan Leffler * 0.3 More posix'ation (handling of ':' at start of shortargs meaning "don't handle * missing argument errors"). * 0.4 A fix and using writef/writefln instead of printf for the debug and the tests * 0.5 A fix and using dmd 1.014. Fixed by jinheking [jinheking@gmail.com] */ /++++++++++++++++++++++++ + Our function. + Example: + + ------------------------------------------------------ + import getopt; + import std.stdio; + void main(char[][] args) { + args = args[1..args.length]; + getopt_t[] usedoptions; + + char[] shortoptions = "hvtn?"; + char[][5] longoptions; + int iLen=0; + int idxop = -1; + longoptions[][++idxop] = "help"; + longoptions[][++idxop] = "version"; + longoptions[][++idxop] = "traditional"; + longoptions[][++idxop] = "next-generation"; + + try + { + GetOpt getopt =new GetOpt(); + usedoptions=getopt.getopt(args, shortoptions, longoptions); + } catch(GetOptException e){ + writefln("Error in passed options: ", e.msg); + return 1; + } + foreach(getopt_t item; usedoptions){ + + if (item.option == "-h" || item.option == "--help" || item.option == "-?") + { + writefln("``Hello World!'' is a greeting program which wrote by jinheking.\n", + "\n", + + "Usage: hello [OPTIONS]\n", + " -h, --help display this message then exit.\n", + " -?, display this message then exit.\n", + " -v, --version display version information then exit.\n", + "\n", + " -t, --traditional output a greeting message with traditional format.\n", + " -n, --next-generation output a greeting message with next-generation format.\n", + "\n", + "Report bugs to <jinheking@gmail.com>\n"); + } + else if (item.option == "-v" || item.option == "--version"){ + char[] output = item.value; + writefln("Hello World - jinheking's hello world. 0.01 version\n"); + } + else if (item.option == "-t" || item.option == "--traditional"){ + char[] output = item.value; + writefln("Hello, World\n"); + } + else if (item.option == "-n" || item.option == "--next-generation"){ + char[] output = item.value; + writefln("+---------------+\n", + "| Hello, world! |\n", + "+---------------+\n"); + } + iLen++; + } + if(iLen==0){ + writefln("Mr. Watson. Come Here. I need you."); + } +} + ------------------------------------------------------------------------- +/ //TODO: GNU getopt, object oriented interface module getopt; private import std.string; private import std.array; private import std.stdio; enum GetOptError { NoOption = 1, ArgRequired, ArgNotRequired } class GetOptException : Exception { public GetOptError errtype; char[] option; this(char[] msg, GetOptError errtype, char[] option) { this.errtype = errtype; this.option = option; super(msg); } } struct getopt_t { char[] option; char[] value; } class GetOpt{ /*this(inout char[][] args, char[] shortopts, char[][] longopts){ getopt(args,shortopts,longopts); }*/ getopt_t[] getopt(inout char[][] args, char[] shortopts, char[][] longopts) { char[] currentarg; getopt_t save; getopt_t[] retlist; int retlistsize = 0; bool handleerrors = 1; if (shortopts.length > 0) if (shortopts[0] == ':') // Let the app handle the missing argument errors handleerrors = 0; if (longopts.length > 0) if (longopts[0] == "=") handleerrors = 0; while (args.length > 0 && args[0][0] == '-' && args[0] != "-") { if (args[0] == null) { args = args[1..args.length]; continue; } if (args[0] == "--") { args = args[1..args.length]; break; } currentarg = args[0]; args = args[1..args.length]; if (currentarg[0..2] == "--") { currentarg = currentarg[2..currentarg.length]; save = do_longs(longopts, currentarg, args, handleerrors); retlist.length = ++retlistsize; retlist[retlistsize - 1].option = save.option; retlist[retlistsize - 1].value = save.value; debug(getopt) { writefln("LongOpt: ", save.option); writefln("Value: ", save.value == "" ? "No value" : save.value); } } else { currentarg = currentarg[1..currentarg.length]; getopt_t[] savelist; savelist = do_shorts(shortopts, currentarg, args, handleerrors); foreach(getopt_t it; savelist) { retlist.length = ++retlistsize; retlist[retlistsize - 1].option = it.option; retlist[retlistsize - 1].value = it.value; debug(getopt) { writefln("ShortOpt: ", it.option); writefln("Value: ", it.value); writefln(); } } } } return retlist; } getopt_t do_longs(char[][] longopts, char[]str, inout char[][]args, bool handleerrors) { getopt_t ret; char[] optarg, errormsg; int i = find(str, '='); if(i != -1) { // Take the argument and strip the argument part from the option optarg = str[i+1..str.length]; str = str[0..i]; } if(long_has_args(str, longopts)) { if(optarg.length == 0) //No "=", so it's an option after an space { if(args.length == 0) { if(handleerrors) { errormsg = std.string.format("option --", str, " requires argument"); throw new GetOptException(errormsg, GetOptError.ArgRequired, str); } else optarg = ""; } else { if(find(args[0], '-') == 0) { if(handleerrors) { errormsg = std.string.format("option --",str," requires argument"); throw new GetOptException(errormsg, GetOptError.ArgRequired, str); } else optarg = ""; } else { optarg = args[0]; args = args[1..args.length]; } } } } else if(optarg.length > 0 && handleerrors) { errormsg = std.string.format("option --",str," must not have an argument (\"",optarg,"\" supplied)"); throw new GetOptException(errormsg, GetOptError.ArgNotRequired, str); } ret.option = "--"~str; ret.value = optarg.length > 0 ? optarg : ""; return ret; } bool long_has_args(char[]option, char[][]longoptions) { int pos; bool hasopt; char[] origoption; foreach(char[] currentarg; longoptions) { if(currentarg == null)continue; hasopt = 0; if( (pos = find(currentarg, '=')) != -1 ) { origoption = currentarg[0..currentarg.length-1]; hasopt = 1; } else { origoption = currentarg; hasopt = 0; } if(option != origoption) continue; else return hasopt; } char[] errormsg = std.string.format("option --",option," not recognized"); throw new GetOptException(errormsg, GetOptError.NoOption, option); } getopt_t[] do_shorts(char[] shortopts, char[]str, inout char[][]args, bool handleerrors) { getopt_t ret; getopt_t[] retlist; int retlistsize = 0; char[] optarg, errormsg; char opt; while(str.length > 0) { opt = str[0]; str = str[1..str.length]; if(short_has_args(opt,shortopts)) { if(str.length == 0) { if(args.length == 0) { if(handleerrors) { errormsg = std.string.format("option -",opt," requires argument"); throw new GetOptException(errormsg, GetOptError.ArgRequired, str); } else optarg = ""; } else { if(find(args[0], '-') == 0) { if(handleerrors) { errormsg = std.string.format("option -",opt," requires argument"); throw new GetOptException(errormsg, GetOptError.ArgRequired, str); } else { optarg = ""; } } else { optarg = args[0]; args = args[1..args.length]; } } } else { optarg = str; str = ""; } } else { optarg = ""; } retlist.length = ++retlistsize; ret.option = std.string.format("-", opt); ret.value = optarg.length > 0 ? optarg : ""; retlist[retlistsize - 1] = ret; } return retlist; } bool short_has_args(char option, char[]shortoptions) { //for(int i=0;i<shortoptions.length;i++) foreach(int i, char c; shortoptions) { if(option == c) { try { if(shortoptions[i+1] == ':') return 1; else return 0; } catch(ArrayBoundsError) { return 0; } } } char[] stroption = std.string.format(option); char[] errormsg = std.string.format("option -",stroption," not recognized"); throw new GetOptException(errormsg, GetOptError.NoOption, stroption); } } unittest { int idx = -1; int idx2 = -1; debug(getopt) writefln("getopt.getopt.unittest"); char[][] longoptions; longoptions.length = 5; longoptions[++idx2] = "test1"; longoptions[++idx2] = "test2="; longoptions[++idx2] = "test10"; longoptions[++idx2] = "test20="; longoptions[++idx2] = "test30"; char[] shortoptions = "ab:c:d"; char[][] args; args.length = 10; args[++idx] = "--test1"; args[++idx] = "-a"; args[++idx] = "--test2"; args[++idx] = "param2"; args[++idx] = "-bparamb"; args[++idx] = "--test10"; args[++idx] = "--test20=param20"; args[++idx] = "-adc"; args[++idx] = "paramc"; args[++idx] = "nonoptionargument"; debug(getopt) writefln("Args: --test1 -a --test2 param2 -bparamb --test10 --test20=param20 -adc paramc nonoptionargument"); getopt_t[] usedoptions; usedoptions = getopt(args, shortoptions, longoptions); bool hastest1, hasa, hastest2, hastest2param, hasb, hasbparam, hastest10; bool hastest20, hastest20param, hasc, hascparam, hasnooption; foreach(getopt_t item; usedoptions) { if (item.option == "--test1") hastest1 = 1; else if (item.option == "-a") hasa = 1; else if (item.option == "--test2") { hastest2 = 1; if (item.value == "param2") hastest2param = 1; } else if (item.option == "-b") { hasb = 1; if (item.value == "paramb") hasbparam = 1; } else if (item.option == "--test10") hastest10 = 1; else if (item.option == "--test20") { hastest20 = 1; if (item.value == "param20") hastest20param = 1; } else if (item.option == "-c") { hasc = 1; if (item.value == "paramc") hascparam = 1; } } assert(hastest1); assert(hasa); assert(hastest2); assert(hastest2param); assert(hasb); assert(hasbparam); assert(hastest10); assert(hastest20); assert(hastest20param); assert(hasc); assert(hascparam); assert(args[0] == "nonoptionargument"); debug(getopt) writefln("getopt.unittest passed ok"); }
hello.d
import getopt; import std.stdio; void main(char[][] args) { args = args[1..args.length]; getopt_t[] usedoptions; /*foreach(char[] argv;args) { writefln("File: ", argv); } */ char[] shortoptions = "hvtn?"; char[][5] longoptions; int iLen=0; int idxop = -1; longoptions[][++idxop] = "help"; longoptions[][++idxop] = "version"; longoptions[][++idxop] = "traditional"; longoptions[][++idxop] = "next-generation"; try { GetOpt getopt =new GetOpt(); usedoptions=getopt.getopt(args, shortoptions, longoptions); } catch(GetOptException e) { writefln("Error in passed options: ", e.msg); return 1; } foreach(getopt_t item; usedoptions){ if (item.option == "-h" || item.option == "--help" || item.option == "-?") { writefln("``Hello World!'' is a greeting program which wrote by jinheking.\n", "\n", "Usage: hello [OPTIONS]\n", " -h, --help display this message then exit.\n", " -?, display this message then exit.\n", " -v, --version display version information then exit.\n", "\n", " -t, --traditional output a greeting message with traditional format.\n", " -n, --next-generation output a greeting message with next-generation format.\n", "\n", "Report bugs to <jinheking@gmail.com>\n"); } else if (item.option == "-v" || item.option == "--version"){ char[] output = item.value; writefln("Hello World - jinheking's hello world. 0.01 version\n"); } else if (item.option == "-t" || item.option == "--traditional"){ char[] output = item.value; writefln("Hello, World\n"); } else if (item.option == "-n" || item.option == "--next-generation"){ char[] output = item.value; writefln("+---------------+\n", "| Hello, world! |\n", "+---------------+\n"); } iLen++; } if(iLen==0){ writefln("Mr. Watson. Come Here. I need you."); } }
dmd hello.d getopt.d -D
评论
13 楼
jinheking
2007-04-29
谢谢!
我会认真的读的。
我会认真的读的。
12 楼
qiezi
2007-04-29
有时间还是多看看D语法吧,很多东西都有简化写法。比如:
可以简化成:
D在简化语法上做了很多工作,你都不用它,怎么体会出它的好处呢。。
char[][] longoptions; longoptions.length = 5; longoptions[++idx2] = "test1"; longoptions[++idx2] = "test2="; longoptions[++idx2] = "test10"; longoptions[++idx2] = "test20="; longoptions[++idx2] = "test30";
可以简化成:
char[][] longoptions = [ "test1", "test2=", "test10", "test20=", "test30" ];
D在简化语法上做了很多工作,你都不用它,怎么体会出它的好处呢。。
11 楼
qiezi
2007-04-29
忙的问题找管理员吧。。成员应该都可以上传的
10 楼
jinheking
2007-04-29
主要是,我想上传,但是不知道为什么不能够上传。老说忙
还是,我没有这个权限?
还是,我没有这个权限?
9 楼
jinheking
2007-04-29
switch(item.option){ case "-h": case "-?": case "--help": writefln("Version:0.02(2007-04-29)\n", "``Hello World!'' is a greeting program which wrote by jinheking.\n", "\n", "Usage: hello [OPTIONS]\n", " -h, --help display this message then exit.\n", " -?, display this message then exit.\n", " -v, --version display version information then exit.\n", "\n", " -t, --traditional output a greeting message with traditional format.\n", " -n, --next-generation output a greeting message with next-generation format.\n", "\n", "Report bugs to <jinheking@gmail.com>\n"); break; case "-v": case "--version": writefln("Hello World - jinheking's hello world. 0.02 version\n"); break; case "-t": case "--traditional": writefln("Hello, World\n"); break; case "-n": case "--next-generation": writefln("+---------------+\n", "| Hello, world! |\n", "+---------------+\n"); break; default: break; } iLen++; }
8 楼
jinheking
2007-04-29
switch(item.option){
case "-h":
case "-?":
case "--help":
writefln("Version:0.02(2007-04-29)\n",
"``Hello World!'' is a greeting program which wrote by jinheking.\n",
"\n",
"Usage: hello [OPTIONS]\n",
" -h, --help display this message then exit.\n",
" -?, display this message then exit.\n",
" -v, --version display version information then exit.\n",
"\n",
" -t, --traditional output a greeting message with traditional format.\n",
" -n, --next-generation output a greeting message with next-generation format.\n",
"\n",
"Report bugs to <jinheking@gmail.com>\n");
break;
case "-v":
case "--version":
writefln("Hello World - jinheking's hello world. 0.02 version\n");
break;
case "-t":
case "--traditional":
writefln("Hello, World\n");
break;
case "-n":
case "--next-generation":
writefln("+---------------+\n",
"| Hello, world! |\n",
"+---------------+\n");
break;
default:
break;
}
iLen++;
}
case "-h":
case "-?":
case "--help":
writefln("Version:0.02(2007-04-29)\n",
"``Hello World!'' is a greeting program which wrote by jinheking.\n",
"\n",
"Usage: hello [OPTIONS]\n",
" -h, --help display this message then exit.\n",
" -?, display this message then exit.\n",
" -v, --version display version information then exit.\n",
"\n",
" -t, --traditional output a greeting message with traditional format.\n",
" -n, --next-generation output a greeting message with next-generation format.\n",
"\n",
"Report bugs to <jinheking@gmail.com>\n");
break;
case "-v":
case "--version":
writefln("Hello World - jinheking's hello world. 0.02 version\n");
break;
case "-t":
case "--traditional":
writefln("Hello, World\n");
break;
case "-n":
case "--next-generation":
writefln("+---------------+\n",
"| Hello, world! |\n",
"+---------------+\n");
break;
default:
break;
}
iLen++;
}
7 楼
qiezi
2007-04-29
标题应该写成getopt或是命令行解析器。。。
6 楼
qiezi
2007-04-29
现在最缺的就是各平台、库的头文件转到D,其它的包装成类什么的都还好办点。
5 楼
soulmachine
2007-04-29
太长了,不愿看。应该不是win32 下的GUI版的hello world吧
4 楼
oldrev
2007-04-29
看来最有用的项目还是写一份详细的 D 文档
3 楼
jinheking
2007-04-29
谢谢,老大的夸奖
switch可以这样用吗?
又长了新本领
switch可以这样用吗?
又长了新本领
2 楼
oldrev
2007-04-29
真累啊?除了tango,有没有其他转换好的 posix 头文件
1 楼
qiezi
2007-04-29
好东西,不过标题起得不咋好,差点就没进来了。。
例子代码里面的if..else if..可以用switch代替,D语言支持对字符串进行switch:
看起来舒服一些,效率应该是比if更高,因为它在编译时就把case部分排序过了,然后使用二分查找来匹配。详见DMD\src\phobos\internal\switch.d里面_d_switch_string方法。
例子代码里面的if..else if..可以用switch代替,D语言支持对字符串进行switch:
switch(item.option) { case "-v": case "--version: xxxx break; case "-t": case "--traditional": break; defaut: }
看起来舒服一些,效率应该是比if更高,因为它在编译时就把case部分排序过了,然后使用二分查找来匹配。详见DMD\src\phobos\internal\switch.d里面_d_switch_string方法。
相关推荐
虽然在描述中未提及,但在实际开发中,你可以通过集成PrimeFaces来增强"HelloWorld"应用的用户体验,例如使用`<p:commandButton>`实现AJAX操作,或使用`<p:dialog>`显示对话框。 **学习资源** 为了深入学习JSF 2.0...
C++是C语言的增强版,它保留了C语言的效率,同时引入了面向对象编程(OOP)的概念。"Hello, World!"程序是展示基本语法结构的简单示例。在C++中,这个程序通常由包含`#include <iostream>`头文件、`using namespace ...
通过深入研究Joomla Modula HelloWorld,你可以获得构建自定义Joomla模块的实践经验,从而增强你在Joomla生态系统中的开发能力。同时,这也是一个很好的起点,可以帮助你逐步掌握更复杂模块的开发,提升你的网站设计...
本文将深入探讨“android_helloworld”项目,理解Android应用的基础结构,以及如何在Android Studio中创建和运行一个简单的应用。 首先,"android_helloworld"项目的核心是展示Android应用程序的基本构成。Android...
例如,`"name": "Chrome HelloWorld"`会设置扩展的名称。 2. **background.js**:这是后台脚本,它在浏览器启动时运行,并持续存在,负责处理事件和执行非用户交互的任务。在这个Hello World程序中,可能包含了一些...
在本实例"Firefox扩展-HelloWorld"中,我们将探讨如何创建一个基础的Firefox扩展,以此来理解其基本架构和开发过程。 首先,"helloworld.xpi"文件是Firefox扩展的打包格式,类似于其他软件的安装程序。XPI...
还可以利用字符串模板,这是Java 5引入的增强版for循环(foreach)和`String`类的特性: ```java public class HelloWorld { public static void main(String[] args) { String[] parts = {"Hello", "World!"}; ...
用maven构建项目使用spring和multicast广播注册中心方式实现Dubbo入门之hello world(用maven构建项目使用spring和multicast广播注册中心方式实现Dubbo入门之hello world(简单测试版)文档说明以及源码
**Mina入门:Mina版之HelloWorld** Apache Mina是一个开源项目,它提供了一个高度模块化、高性能的网络通信框架。Mina旨在简化网络应用的开发,支持多种传输协议,如TCP、UDP、HTTP、FTP等。在这个“Mina入门:Mina...
C++是C语言的增强版,它支持面向对象编程。输出"Hello, World!"的C++代码如下: ```cpp #include int main() { std::cout << "Hello, World!"; return 0; } ``` 这段代码需要通过编译器(如g++)编译后...
3. **编写代码**:在`src/main/java`目录下,会有一个名为`hudson.plugins.helloworld`的包,包含`HelloWorldBuilder`类,这是插件的核心,定义了构建过程的行为。`HelloWorldPublisher`类可能会负责展示“Hello ...
《MicroBlaze嵌入式系统初探:Hello_World.zip_world》 在探索嵌入式系统的世界时,MicroBlaze是一个不可忽视的重要角色。它是由Xilinx公司开发的一种软核处理器,可在FPGA(Field-Programmable Gate Array)芯片上...
4. **架构级性能增强**: - 由于可以手动编写SQL语句,开发者可以根据具体的业务需求和数据库特性来优化SQL,从而提高应用的整体性能。 5. **SQL语句与程序代码分离**: - 通过将SQL语句从程序代码中分离出来,...
7. **编程实践**:通过分析和运行"Hello World AF",实际操作LabVIEW编程,增强对LabVIEW的理解。 8. **调试技巧**:如果程序包含错误或需要改进的地方,学习如何在LabVIEW中进行调试和优化。 9. **版本控制**:...
`Hello.js`是由DWR自动生成的,它包含了`HelloWorld`类的JavaScript接口。现在,你可以在JavaScript中像调用本地函数一样调用`HelloWorld`的方法: ```javascript Hello.sayHello('World', function(response) { ...
《加密技术探索:Cryptomator 1.5.10 "Hello World" 源码解析》 在当今数字化时代,数据安全成为了我们关注的重要议题。为了保护个人隐私和敏感信息,加密技术扮演了至关重要的角色。本次我们将深入探讨一个名为...
- `public class HelloWorld {`:声明了一个名为`HelloWorld`的公共类。在Java中,类名通常首字母大写,并且公共类的名称应该与文件名相同。 - `public static void main(String[] args) {`:定义了程序的入口点,...
【标题】"helloworld_html_world_halfwaym1i_" 暗示了这是一个关于HTML语言的基础教学项目,可能用于展示“Hello, World!”程序的实现。"halfwaym1i"可能是项目或版本的标识符,但具体含义不明确。 【描述】"this ...
本示例"ipad helloworld demo"正是为了指导开发者如何在iPad上创建并实现一个基础的应用程序,区别于iPhone的开发流程。 首先,我们要明确的是,iPad应用开发是基于iOS SDK中的UIKit框架,这个框架提供了创建用户...
**DWR HelloWorld 指南** DWR (Direct Web Remoting) 是一个开源的Java库,它允许在浏览器和服务器之间进行实时、双向通信,从而实现了JavaScript与Java代码的交互。DWR使得Web应用程序能够像桌面应用那样更新内容...