#!/usr/bin/env python
# 12.01.2007, c
from __future__ import absolute_import
from optparse import OptionParser

import sfepy
from sfepy.base.base import output
from sfepy.base.conf import ProblemConf, get_standard_keywords
from sfepy.homogenization.band_gaps_app import AcousticBandGapsApp
from sfepy.base.plotutils import plt

usage = """%prog [options] filename_in"""

help = {
    'filename' :
    'basename of output file(s) [default: <basename of input file>]',
    'detect_band_gaps' :
    'detect frequency band gaps',
    'analyze_dispersion' :
    'analyze dispersion properties (low frequency domain)',
    'plot' :
    'plot frequency band gaps, assumes -b',
    'phase_velocity' :
    'compute phase velocity (frequency-independet mass only)'
}

def main():
    parser = OptionParser(usage=usage, version="%prog " + sfepy.__version__)
    parser.add_option("-o", "", metavar='filename',
                      action="store", dest="output_filename_trunk",
                      default=None, help=help['filename'])
    parser.add_option("-b", "--band-gaps",
                      action="store_true", dest="detect_band_gaps",
                      default=False, help=help['detect_band_gaps'])
    parser.add_option("-d", "--dispersion",
                      action="store_true", dest="analyze_dispersion",
                      default=False, help=help['analyze_dispersion'])
    parser.add_option("-p", "--plot",
                      action="store_true", dest="plot",
                      default=False, help=help['plot'])
    parser.add_option("--phase-velocity",
                      action="store_true", dest="phase_velocity",
                      default=False, help=help['phase_velocity'])

    options, args = parser.parse_args()
    if options.plot:
        if plt is None:
            output('matplotlib.pyplot cannot be imported, ignoring option -p!')
            options.plot = False
        elif options.analyze_dispersion == False:
            options.detect_band_gaps = True

    if (len(args) == 1):
        filename_in = args[0];
    else:
        parser.print_help(),
        return

    required, other = get_standard_keywords()
    required.remove('equations')
    if not options.analyze_dispersion:
        required.remove('solver_[0-9]+|solvers')
    if options.phase_velocity:
        required.remove('ebc_[0-9]+|ebcs')
    conf = ProblemConf.from_file(filename_in, required, other)

    app = AcousticBandGapsApp(conf, options, 'phonon:')
    opts = conf.options
    if hasattr(opts, 'parametric_hook'): # Parametric study.
        parametric_hook = conf.get_function(opts.parametric_hook)
        app.parametrize(parametric_hook)
    app()

if __name__ == '__main__':
    main()

@@ -75,19 +75,19 @@
 rt%0Afrom 
-opt
+arg
 parse im
@@ -91,22 +91,24 @@
  import 
-Option
+Argument
 Parser%0A%0A
@@ -327,51 +327,8 @@
 lt%0A%0A
-usage = %22%22%22%25prog %5Boptions%5D filename_in%22%22%22%0A%0A
 help
@@ -739,33 +739,103 @@
 r = 
-OptionParser(usage=usage,
+ArgumentParser()%0A    parser.add_argument(%22--version%22, action=%22version%22,%0A                       
  ver
@@ -845,12 +845,15 @@
 n=%22%25
+(
 prog
+)s
  %22 +
@@ -891,22 +891,20 @@
 add_
-option(%22-o%22, %22
+argument(%22-o
 %22, m
@@ -922,16 +922,18 @@
 ename',%0A
+  
         
@@ -1006,32 +1006,34 @@
                 
+  
 default=None, he
@@ -1068,22 +1068,24 @@
 ser.add_
-option
+argument
 (%22-b%22, %22
@@ -1094,24 +1094,26 @@
 band-gaps%22,%0A
+  
             
@@ -1182,32 +1182,34 @@
                 
+ 
+ 
 default=False, h
@@ -1253,22 +1253,24 @@
 ser.add_
-option
+argument
 (%22-d%22, %22
@@ -1298,32 +1298,34 @@
                 
+ 
+ 
 action=%22store_tr
@@ -1348,32 +1348,34 @@
 ze_dispersion%22,%0A
+  
                 
@@ -1443,22 +1443,24 @@
 ser.add_
-option
+argument
 (%22-p%22, %22
@@ -1464,24 +1464,26 @@
 , %22--plot%22,%0A
+  
             
@@ -1540,32 +1540,34 @@
                 
+  
 default=False, h
@@ -1599,22 +1599,24 @@
 ser.add_
-option
+argument
 (%22--phas
@@ -1620,32 +1620,34 @@
 hase-velocity%22,%0A
+  
                 
@@ -1710,32 +1710,34 @@
                 
+  
 default=False, h
@@ -1764,16 +1764,59 @@
 city'%5D)%0A
+    parser.add_argument(%22filename_in%22)%0A    
 %0A    opt
@@ -1823,14 +1823,8 @@
 ions
-, args
  = p
@@ -2099,119 +2099,8 @@
 ue%0A%0A
-    if (len(args) == 1):%0A        filename_in = args%5B0%5D;%0A    else:%0A        parser.print_help(),%0A        return%0A%0A
     
@@ -2332,24 +2332,24 @@
 -9%5D+%7Cebcs')%0A
-
     conf = P
@@ -2369,16 +2369,24 @@
 om_file(
+options.
 filename

use argparse instead of optparse in phonon.py