created.
[chise/perl.git] / Chise_utils / sample.pl
1 #!/usr/bin/perl -w
2
3 use Getopt::Long;  
4 use strict;
5 use vars qw($atr $value $char
6             $opt_map $opt_mapfrom $opt_mapto
7             $opt_atr $opt_allatr $opt_showatr
8             $opt_query
9             $opt_help
10             );
11 use Chise_utils ':all';
12
13 my $usage=<<EOF;
14 Usage: perl $0 [-m <map> | --map <map> |
15                 --mapfrom <map> |
16                 --mapto <map> |
17                 -q <expression> |
18                 --show-attribute |
19                 -h | --help |
20                 -a=<atribuite> <char> |
21                 -aa <char>]
22     -q  <expression>
23       ex. of <expression>
24          total-strokes==8 means
25            the attribute of total-strokes exactly matches to 8.
26          ids-parts=~矢 means
27            the attribute of ids-parts contains the string 矢.
28          total-strokes==8,ids-parts=~矢 means both at the same time.
29     -a  get <attribute> of <char>
30     -aa get all attributes of <char>
31 EOF
32
33 &GetOptions("mapto=s"=>\$opt_mapto,
34             "mapfrom=s"=>\$opt_mapfrom,
35             "m=s"=>\$opt_map,
36             "a=s"=>\$opt_atr,
37             "aa"=>\$opt_allatr,
38             "show-attribute"=>\$opt_showatr,
39             "q"=>\$opt_query,
40             "help",\$opt_help,
41             "h",\$opt_help);
42
43 if($opt_help
44    or !((($opt_map or $opt_mapfrom or $opt_mapto) and @ARGV==0)
45         or($opt_showatr and @ARGV==0)
46         or @ARGV==1
47         )
48    ){
49     print $usage;
50     exit 1;
51 }else{
52     $char=shift or $char="";
53 }
54
55 if($opt_mapfrom){
56     $opt_map="<=".$opt_mapfrom;
57 }elsif($opt_mapto){
58     $opt_map="=>".$opt_mapto;
59 }
60
61 if($opt_map){
62     if(defined(%{$chardb{$opt_map}})){
63         foreach $char (sort keys %{$chardb{$opt_map}}){
64             if($chardb{$opt_map}->{$char}){
65                 print $char,"\t",$chardb{$opt_map}->{$char},"\n";
66             }
67         }
68     }else{
69         print STDERR "No map for $opt_map.\n";
70     }
71 }elsif($opt_allatr){
72     $char=&de_er($char) if($char=~/\d/);
73     print $char,"\n";
74     foreach $atr (sort keys %db){
75         if($chardb{$atr}->{"?$char"}){
76             print "  ",$atr,":",$chardb{$atr}->{"?$char"},"\n";
77         }
78     }
79 }elsif($opt_atr){
80     $char=&de_er($char) if($char=~/\d/);
81     print $char,"\n";
82     print "  ",$opt_atr,":",&get_char_attribute($char,$opt_atr),"\n";
83 }elsif($opt_showatr){
84     foreach $atr (sort keys %db){
85         print $atr,"\n";
86     }
87 }elsif($opt_query){
88     my $query=$char;
89     print join "\n",&get_chars_for($query),"\n";
90 }
91
92 exit 0;