Changeset 893 for dasscm/trunk/usr/bin


Ignore:
Timestamp:
Jun 26, 2010, 2:13:20 PM (14 years ago)
Author:
joergs
Message:

better option handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • dasscm/trunk/usr/bin/dasscm

    r892 r893  
    4141
    4242my %COMMANDS = (
    43     '--help'            => 'help',
    4443    'help'              => 'help',
    4544    'login'             => 'login',
     
    105104        'desc'     => [],
    106105        'params'   => ["PATH_PROD"],
     106        'options'  => [ 'verbose', 'message=s' ],
    107107        'require'  => ["WRITE"],
    108108        'function' => \&add
     
    111111        'desc'     => [],
    112112        'params'   => ["PATH_REPO"],
     113        'options'  => [ 'verbose', 'message=s' ],
    113114        'require'  => ["WRITE"],
    114115        'function' => \&commit
     
    432433}
    433434
     435sub get_command_possible_options( $ )
     436{
     437    my $command = get_command_uniform_name( $_[0] );
     438    my @params  = ();
     439    if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'options'} ) ) {
     440        @params = @{ $COMMAND_DEFINITIONS{$command}{'options'} };
     441    }
     442    return @params;
     443}
     444
    434445sub get_command_requirements( $ )
    435446{
     
    9941005sub help(;@)
    9951006{
    996     if ( @_ == 0 ) {
     1007    if ( not @_ ) {
    9971008        usage();
    9981009    } else {
    999         print "help for @_: ...\n";
     1010        print "help for ", join( " ", @_ ), ": ...\n";
    10001011        usage();
    10011012    }
     
    15621573    my @input            = @_;
    15631574
     1575    # TODO: check global options
     1576
    15641577    if ( $number_arguments <= 1 ) {
    15651578
    15661579        # complete dasscm commands
    15671580        my $input = $input[0] || "";
    1568         foreach my $i ( keys %COMMANDS ) {
    1569             $_ = $i;
    1570             if (m/^$input/) {
    1571 
    1572                 #my $command = get_command_uniform_name($i);
    1573                 #print $command, "\n";
    1574                 print $i, "\n";
    1575             }
    1576         }
     1581        map { m/^$input/ && print $_, "\n" } ( keys %COMMANDS );
    15771582    } else {
    15781583
     
    16591664my $number_arguments = @ARGV;
    16601665
     1666# global options
     1667# stops at first non-option
     1668Getopt::Long::Configure( 'require_order' );
     1669if( not GetOptions( \%options, 'help', 'verbose' ) ) {
     1670    usage();
     1671    exit $RETURN_NOK;
     1672}
     1673
     1674# set verbose to command line option
     1675$verbose = $options{'verbose'};
     1676
     1677if( $options{'help'} ) {
     1678    help(@ARGV);
     1679    exit;
     1680}
     1681
    16611682# get subcommand and remove it from @ARGV
    16621683if( defined( $ARGV[0] ) ) {
     
    16641685    shift @ARGV;
    16651686}
     1687
    16661688if( not defined($command) ) {
    16671689        usage();
    1668         $return_code = $RETURN_NOK;
    1669 } else {
    1670     $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
    1671     $DASSCM_REPOSITORY_NAME       = $config->{'DASSCM_REPOSITORY_NAME'};
    1672 
    1673     # TODO: check variables
    1674     $DASSCM_SVN_REPOSITORY =
    1675       $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
    1676 
    1677     $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
    1678     $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
    1679 
    1680     #
    1681     # if a user is given by dasscm configuration file, we use it.
    1682     # Otherwise we expect that read-only account is configured
    1683     # as local subversion configuration.
    1684     # If this is also not the case,
    1685     # user is required to type username and password.
    1686     # This will be stored as local subversion configuration thereafter.
    1687     #
    1688     if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
    1689         $svnCheckoutCredentials =
    1690           " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
    1691     }
    1692 
    1693     $DASSCM_PERMISSION_FILE = $config->{'DASSCM_PERMISSION_FILE'}
    1694       || "/etc/permissions.d/dasscm.permission_backup";
    1695 
     1690        exit $RETURN_NOK;
     1691}
     1692
     1693$DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
     1694$DASSCM_REPOSITORY_NAME       = $config->{'DASSCM_REPOSITORY_NAME'};
     1695
     1696# TODO: check variables
     1697$DASSCM_SVN_REPOSITORY =
     1698    $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
     1699
     1700$DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
     1701$DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
     1702
     1703#
     1704# if a user is given by dasscm configuration file, we use it.
     1705# Otherwise we expect that read-only account is configured
     1706# as local subversion configuration.
     1707# If this is also not the case,
     1708# user is required to type username and password.
     1709# This will be stored as local subversion configuration thereafter.
     1710#
     1711if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
     1712    $svnCheckoutCredentials =
     1713        " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
     1714}
     1715
     1716$DASSCM_PERMISSION_FILE = $config->{'DASSCM_PERMISSION_FILE'}
     1717    || "/etc/permissions.d/dasscm.permission_backup";
     1718
     1719# check for command options
     1720my @cmd_options = get_command_possible_options( $command );
     1721if( @cmd_options ) {
    16961722    # get command line options and store them in options hash
    1697     my $result = GetOptions( \%options, 'verbose', 'message=s' );
     1723    my $result = GetOptions( \%options, @cmd_options );
    16981724
    16991725    # print options
     
    17011727        print "${option}: $options{$option}\n";
    17021728    }
    1703 
    1704     # set verbose to command line option
    1705     $verbose = $options{'verbose'};
    1706 
    1707     #
    1708     # action accordinly to command are taken
    1709     #
    1710     &{ get_command_function($command) }(@ARGV);
    1711 }
     1729}
     1730
     1731#
     1732# action accordinly to command are taken
     1733#
     1734&{ get_command_function($command) }(@ARGV);
    17121735
    17131736exit $return_code;
Note: See TracChangeset for help on using the changeset viewer.