+import optparse
+from pbs import dd
+
+
+class FatalError(Exception):
+ pass
+
+
+def check_writable_dir(option, opt_str, value, parser):
+ if not os.path.isdir(value):
+ raise OptionValueError("%s is not a valid directory name" % value)
+ setattr(parser.values, option.dest, value)
+
+
+def parse_options(input_args):
+ usage = "Usage: %prog [options] <input_media> <name>"
+ parser = optparse.OptionParser(version=version, usage=usage)
+
+ parser.add_option("-o", "--outdir", type="string", dest="outdir",
+ default=".", action="callback", callback=check_writable_dir,
+ help="Output files to DIR [default: working dir]",
+ metavar="DIR")
+
+ parser.add_option("-f", "--force", dest="force", default=False,
+ action="store_true", help="Overwrite output files if they exist")
+
+ parser.add_option("--no-shrink", dest="shrink", default=True,
+ help="Don't shrink any partition before extracting the image",
+ action="store_false")
+
+ options, args = parser.parse_args(input_args)
+
+ if len(args) != 2:
+ parser.error('input media or name are missing')
+ options.source = args[0]
+ options.name = args[1]
+
+ if not os.path.exists(options.source):
+ parser.error('Input media is not accessible')
+
+ return options