ã¯ããã«
ãC#ãVB.NETãç¨ãããã¨ã§æ軽ã«GUIã¢ããªã±ã¼ã·ã§ã³ãä½ããããã«ãªã£ãã¨ã¯ãããã¡ãã£ã¨ãããã¼ã«ãæ¸ãã¨ããªã©ã¯ãæ軽ãããCè¨èªã使ã£ãCUIã®ããã°ã©ã ã«ãã¦ãã¾ããã¨ãå¤ãã®ã§ã¯ãªããã¨æãã¾ãããã®ãããªCUIãã¼ã¹ã®ãã¼ã«ãä½ãã¨ãããã使ãã®ã«é¢åãªå¦çã¨ãã¦ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ï¼ä»¥ä¸ãªãã·ã§ã³ï¼ã®è§£æãæãããã¾ãã
ãããã§æ¬ç¨¿ã§ã¯ãPC-UNIXç³»ã®ç°å¢ã§ãã使ãããgetopt
é¢æ°ãCè¨èªãã使ãæ§åãç´¹ä»ããªãããåæ§ã®æ¹æ³ãWindowsä¸ã§å®ç¾ããããã®æ¹æ³ã«ã¤ãã¦èª¬æãã¾ãã
対象èªè
- Cè¨èªãç解ãã¦ãã¦ã
getopt
é¢æ°ã使ã£ããªãã·ã§ã³ã®è§£ææ¹æ³ãç¥ãããã¨ããæ¹
ããªãããµã³ãã«ããã°ã©ã ã¯ãã¹ã¦Cè¨èªã§æ¸ããã¦ãããDebian GNU/Linux + gcc 4.3ã§ã³ã³ãã¤ã«ã¨åä½ç¢ºèªãè¡ã£ã¦ãã¾ããã¾ããWindowsåãã«æ¸ããããã°ã©ã ã«ã¤ãã¦ã¯ãWindows XPï¼Professionalï¼ SP3 + Visual Studio 2008ãç¨ãã¦ã³ã³ãã¤ã«ããã³åä½ã®ç¢ºèªãè¡ã£ã¦ãã¾ãã
ã·ã³ãã«ãªãªãã·ã§ã³ã®è§£æ
ãgetopt
ã使ã£ããªãã·ã§ã³ã®è§£ææ¹æ³ãè¦ãåã«ããããããªãã·ã§ã³ã®è§£æã¨ã¯ä½ããã¦ããã®ãã«ã¤ãã¦èãã¦ããããã¨æãã¾ãã
ãé¡æã¨ãã¦ãå¼æ°ã«æå®ããããã¡ã¤ã«ããä¸æåãã¤ãã¼ã¿ãåãåºããæ¨æºåºåã«åºåããã¨ããåç´ãªããã°ã©ã ãèãã¾ãï¼ãªã¹ã1ï¼ã
/* ãã¡ã¤ã«ã®å 容ãæ¸ãåºãã ãã®ã·ã³ãã«ãªããã°ã©ã */ #include <stdio.h> int main(int argc, char *argv[]) { int c; FILE *fp; if( *++argv ) { if( (fp = fopen( *argv, "r" ) ) == NULL ) { fprintf( stderr, "Can't opened fle.\n" ); } else { while( ( c = getc(fp) ) != EOF ) { putchar(c); } fclose( fp ); } } else { printf( "Usage : command [filename]\n" ); } return 0; }
ãããã§ããã®ããã°ã©ã ã«-n
ãªãã·ã§ã³ãã¤ãã¦å®è¡ããã¨ãæ¹è¡è¨å·ãç¡è¦ãã¦è¡¨ç¤ºããããã«æ¹é ãããã¨ãã¾ããããããã¨ããªã¹ã2ã«ç¤ºããããªå½¢ã§ããªãã·ã§ã³ã解æããå¿
è¦ãªå¦çãè¡ãã³ã¼ããæ¸ããã¨ã«ãªãã¾ãã
/* -nãªãã·ã§ã³ãæå®ãããããæ¹è¡ãç¡è¦ããããã«ããããã°ã©ã */ #include <stdio.h> int main(int argc, char *argv[]) { int c; int flg = 0; FILE *fp; if( *++argv ) { if( !strcmp( *argv, "-n" ) ) { flg = 1; ++argv; } if( (fp = fopen( *argv, "r" ) ) == NULL ) { fprintf( stderr, "Can't opened fle.\n" ); } else { while( ( c = getc(fp) ) != EOF ) { if( flg ) { if( c != '\n' ) putchar(c); } else { putchar(c); } } putchar('\n'); fclose( fp ); } } else { printf( "Usage : command [filename]\n" ); } return 0; }
ãã覧ããã ãã°åããã¨ããããªãã·ã§ã³ã®è§£æã¨ããã®ã¯ãçªãè©°ãã¦ããã°
- ãªãã·ã§ã³ã¨ãªãå¼æ°ãããã®ãï¼
- ããå ´åãããã¯æ£ããå¼æ°ãï¼
ããªã©ããã§ãã¯ããããã«å¿ãã¦ãã©ã°ãèµ·ãããããã¡ãã»ã¼ã¸ã®è¡¨ç¤ºå¦çãããã¨ããã®ã主ä½ã«ãªãã¾ããåºæ¬çã«ã¯ãgetopt
ãªã©ã®ãªãã·ã§ã³è§£ææ©è½ã使ã£ãå ´åã«ããã®ä¸ã§è¡ããã¦ããæä½ãããã¨åãã§ããã§ããã°ãã³ã¼ãã¯æ±ããªãã¾ããããã®èª¿åã§if
ã«ããåå²ãå¢ããã¦ããã°ãgetopt
ãªã©ã使ãã¾ã§ããªãã5åã6åã®æ©è½æ¡å¼µã§ããã°ç°¡åã«è¡ããããªæ°ããã¾ãã
ããããããªãã·ã§ã³ãããã«å¼æ°ãã¨ãå¿
è¦æ§ãåºã¦ãããããããªãã·ã§ã³ã®å¼æ°ãçç¥å¯è½ã ã£ããããªãã·ã§ã³ã®æå®ãããé åºã決ã¾ã£ã¦ããªãå ´åãèããã¨ã©ãã§ãããï¼ æ³åããã ãã§é¢åãªæãããã¾ããããã©ãgetopt
ã使ãã°ããããã£ãé¢åãªè¦æ±ã«ããã¡ãã¨çãã¦ããããªãã·ã§ã³ã®è§£æãå¯è½ãªã®ã§ãã