#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# (C) 2017  Jean Nassar
# Released under MIT
from __future__ import print_function
import os
import sys

import rosbag


BAG_FOLDER = "bagfiles"
ROSBAG_TOPICS = [
    "/ardrone/ground_pose",
    "/ardrone/imu",
    "/ardrone/navdata",
    "/ardrone/odometry",
    "/ardrone/past_image",
    "/ardrone/past_pose",
    "/ardrone/pose",
    "/ardrone/slow_image_raw",
    "/ardrone/tracked",
    "/ardrone/arrived",
    "/rosout",
    "/rosout_agg",
]


def parse_args():
    if len(sys.argv) != 3:
        print("Please have experiment type (0 for no SPIRIT, 1 for SPIRIT only,"
              " 2 for both camera and SPIRIT), experimenter id (0 to 99), as "
              "arguments.")
        sys.exit(1)
    _, experiment_type, experimenter_id = sys.argv
    return experiment_type, experimenter_id


def get_filename(experiment_type, experimenter_id):
    run_number = 0
    filename_base = os.path.join(BAG_FOLDER, "experiment-{:d}_user-{:02d}_run-"
        .format(int(experiment_type), int(experimenter_id)))
    while os.path.exists(filename_base + "{:02d}.bag".format(run_number)):
        run_number += 1

    return filename_base + "{:02d}".format(run_number)


def record(arguments):
    rosbag.rosbag_main.record_cmd(arguments)


def main():
    experiment_type, experimenter_id = parse_args()
    filename = get_filename(experiment_type, experimenter_id)
    arguments = ROSBAG_TOPICS + ["-O", filename]
    record(arguments)


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print("\nUser exit")

@@ -134,18 +134,24 @@
 ort 
-os
+argparse
 %0Aimport 
 sys%0A
@@ -146,18 +146,17 @@
 %0Aimport 
-sy
+o
 s%0A%0Aimpor
@@ -522,59 +522,149 @@
     
-if len(sys.argv) != 3:%0A        print(%22Please have e
+parser = argparse.ArgumentParser(description=%22Record bag files.%22)%0A    parser.add_argument(%22experiment_type%22, action=%22store%22,%0A        help=(%22E
 xper
@@ -677,25 +677,28 @@
 type
+:
  
-(
 0 for 
-no SPIRIT
+camera alone
 , 1 
@@ -712,14 +712,23 @@
 RIT 
-only,%22
+alone, and 2%22 +
 %0A   
@@ -743,20 +743,13 @@
    %22
- 2
  for 
-both 
 came
@@ -765,185 +765,155 @@
 IRIT
-), experimenter id (0 to 99), as %22%0A              %22arguments.%22)%0A        sys.exit(1)%0A    _, experiment_type, experimenter_id = sys.argv%0A    return experiment_type, experimenter_id
+ together.%22))%0A    parser.add_argument(%22experimenter_id%22, action=%22store%22,%0A        help=%22Experimenter ID, as an int.%22)%0A    return parser.parse_args()
 %0A%0A%0Ad
@@ -915,24 +915,29 @@
 )%0A%0A%0Adef get_
+next_
 filename(exp
@@ -1375,40 +1375,12 @@
     
-experiment_type, experimenter_id
+args
  = p
@@ -1391,16 +1391,17 @@
 _args()%0A
+%0A
     file
@@ -1411,16 +1411,21 @@
 e = get_
+next_
 filename
@@ -1417,32 +1417,37 @@
 t_next_filename(
+args.
 experiment_type,
@@ -1439,32 +1439,37 @@
 xperiment_type, 
+args.
 experimenter_id)
@@ -1469,16 +1469,17 @@
 ter_id)%0A
+%0A
     argu

Use argparse instead of custom parsing.